PHP Curl, Fsockopen function

Source: Internet
Author: User
Here is a direct example of using curl and fsockopen, which you can use as an example, or directly as a packaged function.

The Curl function uses code

 Public functionxcurl($url,$ref=null,$post=array(),$ UA="mozilla/5.0 (X11; Linux x86_64; Rv:2.2a1pre) gecko/20110324 firefox/4.2a1pre ",$print=false) {$ch= Curl_init (); curl_setopt ($ch, Curlopt_autoreferer,true);if(!Empty($ref) {curl_setopt ($ch, Curlopt_referer,$ref); } curl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_header,0); curl_setopt ($ch, Curlopt_followlocation,1); curl_setopt ($ch, Curlopt_returntransfer,1);if(!Empty($ua) {curl_setopt ($ch, Curlopt_useragent,$ua); }if(Count ($post) >0) {curl_setopt ($ch, Curlopt_post,1); curl_setopt ($ch, Curlopt_postfields,$post); }$output= Curl_exec ($ch); Curl_close ($ch);if($print) {Print($output); }Else{return$output; }}

The Fsockopen function uses code:

 Public functioncurl_request_async($url, $params, $type =' GET ') {//Set Referer$referer=$_server[' Http_host '];foreach($params as$key= &$val) {if(Is_array ($val))$val= Implode (',',$val);$post _params[] =$key.'='. UrlEncode ($val); }$post _string= Implode (' & ',$post _params);$parts=parse_url ($url); @$fp= Fsockopen ($parts[' Host '],isset($parts[' Port '])?$parts[' Port ']: the,$errno,$errstr,2);if(!$fp){Echo"$errstr ($errno)
\ n "; }Else{if(' GET '==$type)$parts[' path '] .='?'.$post _string;$out="$type".$parts[' path ']."http/1.1\r\n";$out.="Host:".$parts[' Host ']."\ r \ n";$out.="Referer:".$referer."\ r \ n";$out.="content-type:application/x-www-form-urlencoded\r\n";$out.="connection:close\r\n\r\n";//Data goes in the request body for a POST requestif(' POST '==$type&&isset($post _string))$out.=$post _string; Fwrite$fp,$out); Fclose$fp); } }

Just from the function above, the difference between the use is that the important point is:
Curl requests that a URL wait for the result to return, and Fsockopen does not need to return, and continues executing the code after the request is sent. Unless you use Fsockopen, you need to print a request to return the results.

Copyright NOTICE: Reprint Please specify source: HTTP://BLOG.CSDN.NET/M0SH1

The above describes the PHP curl, Fsockopen function, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.