|
<? 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 the header information is displayed
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); // sets the curl timeout time in 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 = '', $ 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;
}
}
?>
Usage
GET usage:
$ Curl = new Curl ();
$ Curl-> get ('HTTP: // www.111cn.net /');
POST usage
$ Curl = new Curl ();
$ Curl-> get ('HTTP: // www.111cn.net/', 'P = 1 & time = 0 ′); |