The examples in this article describe the Curl.class.php usage of the Curl encapsulation class implemented by PHP. Share to everyone for your reference. as follows:
<?php//curl class 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 header information curl_setopt ($ch, Curlopt_header, false);
curl_setopt ($ch, Curlopt_returntransfer, 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 number of seconds if (strlen ($userAgent)) {curl_setopt ($ch, Curlopt_useragent, $user
Agent);
} 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::exe
Cute (' 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 your PHP program design.