: This article mainly introduces CURL encapsulation in yii11. For more information about PHP tutorials, see. The flexible extension of the Yii Framework is favored by companies. Therefore, yii is used in the project. to be compatible with the original system, yii1.1 is still selected.
Here we will not talk about the features of yii, but mainly about the re-encapsulation of curl when yii is used.
Let's take a 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 CT:'), CURLOPT_HEADER => '',),),
True, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HEADER => false, CURLOPT_VERBOSE => true, CURLOPT_AUTOREFERER => true, CURLOPT_CONNECTTIMEOUT => 30, CURLOPT_TIMEOUT => 30, limit => 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-> options [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 ));} /*** special post request for weimob internal interface ** supports multi-dimensional 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 sending method ** is used for post without key parameters, post files, etc. */public function post ($ url, $ data = array ()) {$ this-> setOption (CURLOPT_POST, true); $ this-> setOption (CURLOPT_POSTFIELDS, $ data); return $ this-> exec ($ url);} public function 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 ($ u Rl, $ 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 ($ h Eader) {$ 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"); 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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces the CURL re-encapsulation in yii11, including some content, and hope to be helpful to friends who are interested in PHP tutorials.