Rumah >pembangunan bahagian belakang >tutorial php >php curl , fsockopen 函数
下面直接给出 使用 curl 和 fsockopen 的例子,你可以当成使用实例,也可以直接当作封装好的函数直接使用。
curl函数使用代码
<code><span>public</span><span><span>function</span><span>xcurl</span><span>(<span>$url</span>,<span>$ref</span>=null,<span>$post</span>=array<span>()</span>,<span>$ua</span>=<span>"Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre"</span>,<span>$print</span>=false)</span> {</span><span>$ch</span> = curl_init(); curl_setopt(<span>$ch</span>, CURLOPT_AUTOREFERER, <span>true</span>); <span>if</span>(!<span>empty</span>(<span>$ref</span>)) { curl_setopt(<span>$ch</span>, CURLOPT_REFERER, <span>$ref</span>); } curl_setopt(<span>$ch</span>, CURLOPT_URL, <span>$url</span>); curl_setopt(<span>$ch</span>, CURLOPT_HEADER, <span>0</span>); curl_setopt(<span>$ch</span>, CURLOPT_FOLLOWLOCATION, <span>1</span>); curl_setopt(<span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>1</span>); <span>if</span>(!<span>empty</span>(<span>$ua</span>)) { curl_setopt(<span>$ch</span>, CURLOPT_USERAGENT, <span>$ua</span>); } <span>if</span>(count(<span>$post</span>) > <span>0</span>){ curl_setopt(<span>$ch</span>, CURLOPT_POST, <span>1</span>); curl_setopt(<span>$ch</span>, CURLOPT_POSTFIELDS, <span>$post</span>); } <span>$output</span> = curl_exec(<span>$ch</span>); curl_close(<span>$ch</span>); <span>if</span>(<span>$print</span>) { <span>print</span>(<span>$output</span>); } <span>else</span> { <span>return</span><span>$output</span>; } } </code>
fsockopen 函数使用代码:
<code><span>public</span><span><span>function</span><span>curl_request_async</span><span>(<span>$url</span>, <span>$params</span>, <span>$type</span>=<span>'GET'</span>)</span> {</span><span>// set referer</span><span>$referer</span> = <span>$_SERVER</span>[<span>'HTTP_HOST'</span>]; <span>foreach</span> (<span>$params</span><span>as</span><span>$key</span> => &<span>$val</span>) { <span>if</span> (is_array(<span>$val</span>)) <span>$val</span> = implode(<span>','</span>, <span>$val</span>); <span>$post_params</span>[] = <span>$key</span>.<span>'='</span>.urlencode(<span>$val</span>); } <span>$post_string</span> = implode(<span>'&'</span>, <span>$post_params</span>); <span>$parts</span>=parse_url(<span>$url</span>); @<span>$fp</span> = fsockopen(<span>$parts</span>[<span>'host'</span>], <span>isset</span>(<span>$parts</span>[<span>'port'</span>])?<span>$parts</span>[<span>'port'</span>]:<span>80</span>, <span>$errno</span>, <span>$errstr</span>, <span>2</span>); <span>if</span>(!<span>$fp</span>){ <span>echo</span><span>"$errstr ($errno)<br>\n"</span>; }<span>else</span>{ <span>if</span>(<span>'GET'</span> == <span>$type</span>) <span>$parts</span>[<span>'path'</span>] .= <span>'?'</span>.<span>$post_string</span>; <span>$out</span> = <span>"$type "</span>.<span>$parts</span>[<span>'path'</span>].<span>" HTTP/1.1\r\n"</span>; <span>$out</span>.= <span>"Host: "</span>.<span>$parts</span>[<span>'host'</span>].<span>"\r\n"</span>; <span>$out</span>.= <span>"Referer: "</span>.<span>$referer</span>.<span>"\r\n"</span>; <span>$out</span>.= <span>"Content-Type: application/x-www-form-urlencoded\r\n"</span>; <span>$out</span>.= <span>"Connection: Close\r\n\r\n"</span>; <span>// Data goes in the request body for a POST request</span><span>if</span> (<span>'POST'</span> == <span>$type</span> && <span>isset</span>(<span>$post_string</span>)) <span>$out</span>.= <span>$post_string</span>; fwrite(<span>$fp</span>, <span>$out</span>); fclose(<span>$fp</span>); } }</code>
仅仅 从如上的函数,使用上的区别其中重要的一点就是:
curl 请求某个Url 需要等待结果返回,而 fsockopen 不需要返回,发送请求后就继续执行代码。除非你使用 fsockopen 需要打印请求后返回的结果。
版权声明:转载请注明出处:http://blog.csdn.net/m0sh1
以上就介绍了php curl , fsockopen 函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。