下面直接給出 使用 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教程有興趣的朋友有幫助。