Examples of using curl and fsockopen are given directly below. You can use them as examples or directly use them as encapsulated functions.
curl function usage code
<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 function usage code:
<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>
Only from the above functions, the most important difference in usage is:
Curl requests a certain Url and needs to wait for the result to be returned, while fsockopen does not need to return and continues to execute the code after sending the request. Unless you use fsockopen you need to print the results returned after the request.
Copyright Statement: Please indicate the source for reprinting: http://blog.csdn.net/m0sh1
The above has introduced the php curl and fsockopen functions, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.