PHP's Curl wrapper class

Source: Internet
Author: User

I have been doing the crawler-related, each time to write a series of curl_setopt () function is too cumbersome, I encapsulated the following Curl request class.

<?PHP/** * @author freephp * @date 2015-11-13 **/classMycurl {Private Static  $url= ";//URL to access    Private Static $ORIURL= ";//referer URL    Private Static $data=Array();//data that may be emitted post,put    Private   Static $method;//access mode, default is GET request         Public Static functionSend$url,$data=Array(),$method= ' Get ') {        if(!$url)Exit(' url can not ' is null '); Self::$url=$url; Self::$method=$method; $URLARR=Parse_url($url); Self::$ORIURL=$URLARR[' Scheme ']. ':/ /‘.$URLARR[' Host ']; Self::$data=$data; if( !In_array( Self::$method,Array(' Get ', ' post ', ' Put ', ' delete ')             )           ) {                    Exit(' Error request method type! '); }                        $func= Self::$method. ' Request '; returnSelf::$func(Self::$url); }    /** * Base Initiates curl request function * @param int $is _post is a POST request*/    Private  functionDoRequest ($is _post= 0) {        $ch= Curl_init ();//Initialize Curlcurl_setopt ($ch, Curlopt_url, Self::$url);//crawl specified Web pagecurl_setopt ($ch, Curlopt_autoreferer,true); //source must be set to come from the sitecurl_setopt ($ch, Curlopt_referer, Self::$ORIURL); curl_setopt ($ch, Curlopt_returntransfer, 1);//request result As string and output to screen        if($is _post= = 1) curl_setopt ($ch, Curlopt_post,$is _post);//Post Submission Method        if(!Empty(Self::$data) ) { self::$data= self::d ealpostdata (self::$data); curl_setopt ($ch, Curlopt_postfields, Self::$data); }                $data= Curl_exec ($ch);//Run CurlCurl_close ($ch); return $data; }    /** * Initiate a GET request*/     Public functiongetrequest () {returnSelf::d orequest (0); }    /** * Initiate POST request*/     Public functionpostrequest () {returnSelf::d orequest (1); }    /** * handle transmitting data that initiates a non-GET request * * @param array $postData*/     Public functionDealpostdata ($postData) {        if(!Is_array($postData))Exit(' Post data should be array '); foreach($postData  as $k=$v) {            $o.= "$k=" .UrlEncode($v) . "&"; }        $postData=substr($o, 0,-1); return $postData; }    /** * Initiate put request*/     Public functionPutrequest ($param) {        returnSelf::d orequest (2); }        /** * Initiate a DELETE request*/     Public functionDeleterequest ($param) {        returnSelf::d orequest (3); }    }/*$curl = new Mycurl (' http://www.jumei.com ', Array (), ' get '), $res = $curl->send ();*/$res= Mycurl::send (' http://www.ipip.net/ip.html ',Array(' IP ' = ' 61.142.206.145 '), ' post ');Var_dump($res); die();

In order to reduce the unnecessary consumption of the memory heap, I only provide static methods to client calls, considering the possible and high concurrency of cyclic calls. For a single function of the class, the Mycurl only makes requests and returns data, handing over the processing of the returns to other code.

PHP's Curl wrapper class

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.