This article mainly introduces the Curl encapsulation class Curl implemented by php. class. php Usage, detailed analysis of the Curl encapsulation Class Definition and related usage skills in the form of a complete instance, has some reference value, you can refer to the example in this article to describe the Curl encapsulation class Curl implemented by php. class. php usage. 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_R ETURNTRANSFER, 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, CURLOPT_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 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_close ($ 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 = '', $ pas Sword = '') {$ 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.