This article mainly introduces the Curl encapsulation class Curl implemented by php. class. php usage, detailed analysis of Curl encapsulation class definition and related usage skills in the form of a complete instance, with some reference value, friends who need can
This article mainly introduces the Curl encapsulation class Curl implemented by php. class. php usage, detailed analysis of Curl encapsulation class definition and related usage skills in the form of a complete instance, with some reference value, friends who need can
This example describes the Curl encapsulation class Curl. class. php implemented by php. Share it with you for your reference. The details are as follows:
<? Php // curl class Curl {function Curl () {return true;} function execute ($ method, $ url, $ fields = '', $ userAgent = '', $ httpHeaders = '', $ username ='', $ password = '') {$ ch = Curl: create (); if (false = $ ch) {return false;} if (is_string ($ url) & strlen ($ url) {$ ret = curl_setopt ($ ch, CURLOPT_URL, $ url );} else {return false;} // whether to display the header information curl_setopt ($ ch, CURLOPT_HEADER, false); // curl_setopt ($ ch, CURLOPT_RETU RNTRANSFER, true); if ($ username! = '') {Curl_setopt ($ ch, CURLOPT_USERPWD, $ username. ':'. $ password) ;}$ method = strtolower ($ method); if ('post' ==$ method) {curl_setopt ($ ch, CURLOPT_POST, true ); if (is_array ($ fields) {$ sets = array (); foreach ($ fields AS $ key => $ val) {$ sets [] = $ key. '= '. urlencode ($ val) ;}$ fields = implode ('&', $ sets);} curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields );} else if ('put' = $ method) {curl_setopt ($ ch, CURLOP T_PUT, true);} // curl_setopt ($ ch, CURLOPT_PROGRESS, true); // curl_setopt ($ ch, CURLOPT_VERBOSE, true); // curl_setopt ($ ch, CURLOPT_MUTE, false); curl_setopt ($ ch, CURLOPT_TIMEOUT, 10); // set the curl timeout seconds if (strlen ($ userAgent) {curl_setopt ($ ch, CURLOPT_USERAGENT, $ userAgent );} if (is_array ($ httpHeaders) {curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ httpHeaders);} $ ret = curl_exec ($ ch); if (curl_errno ($ ch) {curl_cl Ose ($ ch); return array (curl_error ($ ch), curl_errno ($ ch);} else {curl_close ($ ch); if (! Is_string ($ ret) |! Strlen ($ ret) {return false;} return $ ret;} function post ($ url, $ fields, $ userAgent = '', $ httpHeaders = '', $ username = '', $ password ='') {$ ret = Curl: execute ('post', $ url, $ fields, $ userAgent, $ httpHeaders, $ username, $ password); if (false ===$ ret) {return false;} if (is_array ($ ret) {return false;} return $ ret ;} function get ($ url, $ userAgent = '', $ httpHeaders ='', $ username = '', $ password = '') {$ Ret = Curl: execute ('get', $ url,'', $ userAgent, $ httpHeaders, $ username, $ password ); if (false ===$ ret) {return false;} if (is_array ($ ret) {return false;} return $ ret;} function create () {$ ch = null; if (! Function_exists ('curl _ init ') {return false;} $ ch = curl_init (); if (! Is_resource ($ ch) {return false;} return $ ch ;}}?>
GET usage:
$ Curl = new Curl (); $ curl-> get ('HTTP: // www.XXX.com /');
POST usage:
$ Curl = new Curl (); $ curl-> get ('HTTP: // www.XXX.com/', 'P = 1 & time = 0 ');
I hope this article will help you with php programming.
,