Phpcurl, fsockopen function

Source: Internet
Author: User
: This article mainly introduces phpcurl and fsockopen functions. For more information about PHP tutorials, see. The following example shows how to use curl and fsockopen. you can use it as an instance or as an encapsulated function.

Code used by the curl function

publicfunctionxcurl($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;     }}

Code used for the fsockopen function:

publicfunctioncurl_request_async($url, $params, $type='GET')    {// set referer$referer = $_SERVER['HTTP_HOST'];        foreach ($paramsas$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']:80, $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); } }

The differences between the use of the above functions are as follows:
Curl requests a Url to wait for the result to be returned, and fsockopen does not need to be returned. after sending the request, the code continues to be executed. Unless you use fsockopen to print the results returned after the request.

Copyright Statement: Reprint please indicate the source: http://blog.csdn.net/m0sh1

The above introduces php curl and fsockopen functions, including some content, and hope to help those who are interested in PHP tutorials.

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.