Re-encapsulation of curl in yii11

Source: Internet
Author: User
The flexible extension of yii framework is favored by the company, so the project uses Yii, in order to be compatible with the original system, still chose the yii1.1 version.

The features of Yii are not mentioned here, mainly the re-encapsulation of curl when using Yii.

First look at the Yii configuration file and configure Curl as components in main.php.

       ' Components ' = = Array (                //Curl Library  Call: Yii::app ()->curl         ' curl ' = = Array (            ' class ' = ') ' Ext.curl.Curl ',            ' options ' = = Array (                Curlopt_httpheader = = Array (' Expect: '),                curlopt_header = ' ',            ),        ),

  True, Curlopt_followlocation = True, Curlopt_header = False, Curlopt_verbose        = = True, Curlopt_autoreferer = true, Curlopt_connecttimeout = Curlopt_timeout = =, Curlopt_ssl_verifypeer = False, Curlopt_useragent = ' mozilla/5.0 (compatible; googlebot/2.1;    +http://www.google.com/bot.html) ');        Private function exec ($url) {$this->setoption (Curlopt_url, $url);        $this->response = curl_exec ($this->_ch); if (!curl_errno ($this->_ch)) {if (Isset ($this->options[curlopt_header])) if ($this->option                S[curlopt_header]) {$header _size = Curl_getinfo ($this->_ch, curlinfo_header_size);            Return substr ($this->response, $header _size);        } return $this->response;            } else {throw new CException (Curl_error ($this->_ch));      return false;  }} public function get ($url, $params = Array ()) {$this->setoption (Curlopt_httpget, true);    return $this->exec ($this->buildurl ($url, $params)); }/** * Weimob Internal Interface POST request dedicated * * Support multidimensional array and file Upload * */public function httppostrequest ($url, $data = Array ()) {$this->setoption        (Curlopt_post, True); $this->setoption (Curlopt_postfields, Http_build_query ($data)); return $this->exec ($url);} /** * Curl Native POST request Send method * For need post no key parameter, post file, etc. */public function post ($url, $data = Array ()) {$this-&gt        ; SetOption (Curlopt_post, true); $this->setoption (Curlopt_postfields, $data);    return $this->exec ($url); The public function is put ($url, $data, $params = Array ()) {//write to Memory/temp $f = fopen (' Php://temp        ', ' rw+ ');        Fwrite ($f, $data);        Rewind ($f);        $this->setoption (Curlopt_put, true);        $this->setoption (Curlopt_infile, $f); $this->setoption (Curlopt_infilesize, strlen ($data));    return $this->exec ($this->buildurl ($url, $params));        Public Function Delete ($url, $params = Array ()) {$this->setoption (Curlopt_returntransfer, true);        $this->setoption (curlopt_customrequest, ' DELETE ');    return $this->exec ($this->buildurl ($url, $params));        Public Function BuildUrl ($url, $data = Array ()) {$parsed = Parse_url ($url); Isset ($parsed [' query '])?        Parse_str ($parsed [' query '], $parsed [' Query ']): $parsed [' query '] = Array (); $params = Isset ($parsed [' query '])?        Array_merge ($parsed [' query '], $data): $data; $parsed [' query '] = ($params)? '?' .        Http_build_query ($params): ";        if (!isset ($parsed [' path ')]) {$parsed [' path ']= '/';        } $parsed [' port '] = isset ($parsed [' Port '])? ': '. $parsed [' Port ']: '; return $parsed [' scheme ']. ':/    /'. $parsed [' Host ']. $parsed [' Port ']. $parsed [' path ']. $parsed [' query '];     } Public Function setoptions ($options = Array ()) {   Curl_setopt_array ($this->_ch, $options);    return $this;        Public Function SetOption ($option, $value) {curl_setopt ($this->_ch, $option, $value);    return $this; Public Function setheaders ($header = Array ()) {if ($this->isassoc ($header)) {$out = array ()            ;            foreach ($header as $k = + $v) {$out [] = $k. ': '. $v;        } $header = $out;        } $this->setoption (Curlopt_httpheader, $header);    return $this;    } Private Function Isassoc ($arr) {return Array_keys ($arr)!== range (0, COUNT ($arr)-1);    } public Function GetError () {return curl_error ($this->_ch);    } public Function GetInfo () {return curl_getinfo ($this->_ch);    } public Function GetStatus () {return curl_getinfo ($this->_ch, Curlinfo_http_code); }//Initialize Curl Public function init () {try {$this->_ch = curl_init (); $options = Is_array ($this->options)?            ($this->options + $this->_config): $this->_config;            $this->setoptions ($options);            $ch = $this->_ch;            Close Curl on exit @Yii:: App ()->onendrequest = function () use (& $ch) {curl_close ($ch);        };        } catch (Exception $e) {throw new CException (' Curl not installed ');        }} Public Function Getheaders () {$headers = array ();        $header _text = substr ($this->response, 0, Strpos ($this->response, "\r\n\r\n")); foreach (Explode ("\ r \ n", $header _text) as $i = + $line) {if ($i = = = 0) {$headers [' http_code '            ] = $line;                } else {list ($key, $value) = Explode (': ', $line);            $headers [$key] = $value;    }} return $headers; }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the yii11 in the re-encapsulation of curl, 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.