|
<? Php
/**
* File: curl. class. php
* Createdon: 2014-8-: 34: 01
* Copyright Xiao Hao (C) 2013-2099 copyright
* Www.haosblog.com
*
* CURL encapsulation class. Most operations in this class support chained operations.
*
* Example:
*
* $ Curl = newCurl ($ url );
* $ Curl-> exec ();
* // Send post data
* $ Curl-> post (array ('username' => 'Username')-> exec ();
*/
ClassCurl {
Private $ ch;
Private $ flag_if_have_run = false;
Private $ has_cloase = true;
Publicfunction _ construct ($ url = '', $ forgeIP = false ){
$ This-> init ($ url, $ forgeIP );
}
/**
* Initialize CURL. If the CURL is not closed, disable it first.
*
* @ Paramtype $ url
* @ Return \ Common \ Library \ Curl
*/
Publicfunctioninit ($ url = '', $ forgeIP = false ){
If (! $ This-> has_cloase) {// if the last connection has not ended, close it first
$ This-> close ();
}
If ($ forgeIP) {// forged IP address, the IP address is forged as the visitor's IP address
If (Validate: isIPAddress ($ forgeIP )){
$ Ip = $ forgeIP;
} Else {
$ Ip = $ _ SERVER ['server _ ADDR '];
}
$ This-> set_ip ($ ip );
}
$ This-> ch = curl_init ($ url );
Curl_setopt ($ this-> ch, CURLOPT_RETURNTRANSFER, 1 );
$ This-> has_cloase = false;
Return $ this;
}
PublicfunctionsetUrl ($ url ){
Curl_setopt ($ this-> ch, CURLOPT_URL, $ url );
Return $ this;
}
Publicfunctionclose (){
If (! $ This-> has_close ){
Curl_close ($ this-> ch );
$ This-> has_cloase = true;
}
}
Publicfunction _ destruct (){
$ This-> close ();
}
/**
* Set the page timeout time and support chained operations
*
* @ Paramtype $ timeout
* @ Return \ Common \ Library \ Curl
*/
Publicfunctionset_time_out ($ timeout ){
Curl_setopt ($ this-> ch, CURLOPT_TIMEOUT, intval ($ timeout ));
Return $ this;
}
/**
* Counterfeit source path
*
* @ Paramtype $ referer
* @ Return \ Common \ Library \ Curl
*/
Publicfunctionset_referer ($ referer ){
If (! Empty ($ referer )){
Curl_setopt ($ this-> ch, CURLOPT_REFERER, $ referer );
}
Return $ this;
}
/**
* Set cookie
* This method only sends the cookie information to the remote end and does not save the cookie information returned from the remote end.
*
* @ Paramtype $ cookie_file storage path of the cookie file
* @ Return \ Common \ Library \ Curl
*/
Publicfunctionload_cookie ($ cookie_file ){
$ This-> _ checkCookie ($ cookie_file );
Curl_setopt ($ this-> ch, CURLOPT_COOKIEFILE, $ cookie_file );
Return $ this;
}
/**
* Set cookie
* Send the cookie to the remote end and save the cookie returned by the remote end.
*
* @ Paramtype $ cookie_file
* @ Return \ Common \ Library \ Curl
*/
Publicfunctioncookie ($ cookie_file ){
$ This-> _ checkCookie ($ cookie_file );
Curl_setopt ($ this-> ch, CURLOPT_COOKIEFILE, $ cookie_file );
Curl_setopt ($ this-> ch, CURLOPT_COOKIEJAR, $ cookie_file );
Return $ this;
}
/**
* Set cookie
* In this method, no cookie information is sent. Only the returned cookie is received and saved.
*
* @ Paramtype $ cookie_file
* @ Return \ Common \ Library \ Curl
*/
Publicfunctionsave_cookie ($ cookie_file = ""){
// Set the storage to slow down, for example, a.txt
If (empty ($ cookie_file )){
$ Cookie_file = tempnam ('./', 'cooke ');
}
$ This-> _ checkCookie ($ cookie_file );
Curl_setopt ($ this-> ch, CURLOPT_COOKIEJAR, $ cookie_file );
Return $ this;
}
Privatefunction_checkCookie ($ cookie_file ){
If (! \ Think \ Storage: has ($ cookie_file )){
\ Think \ Storage: put ($ cookie_file ,'');
}
}
/**
* Execute a curl request
*
* @ Returntype
*/
Publicfunctionexec (){
$ Str = curl_exec ($ this-> ch );
$ This-> flag_if_have_run = true;
Return $ str;
}
/**
* Set to send a POST request. This method has not been called and is submitted using the GET method by default.
*
* @ Paramtype $ postData post data. The array and "xxx = 1 & x = 2" formats are supported.
* @ Return \ Common \ Library \ Curl
*/
Publicfunctionpost ($ postData ){
Curl_setopt ($ this-> ch, CURLOPT_POST, 1 );
// Echo ($ postQuery); die;
Curl_setopt ($ this-> ch, CURLOPT_POSTFIELDS, $ postData );
Return $ this;
}
/**
* Obtain curl information
*
* @ Returntype
* @ ThrowsException
*/
Publicfunctionget_info (){
If ($ this-> flag_if_have_run = true ){
Returncurl_getinfo ($ this-> ch );
} Else {
ThrownewException (" }
}
/**
* Set proxy
*
* @ Paramtype $ proxy
* @ Return \ Common \ Library \ Curl
*/
Publicfunctionset_proxy ($ proxy ){
// Set proxy, for example, '68. 119.83.81: 8080'
Curl_setopt ($ this-> ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5 );
Curl_setopt ($ this-> ch, CURLOPT_PROXY, $ proxy );
Return $ this;
}
/**
* Set the requested IP address
*
* @ Paramtype $ ip
* @ Returntype
*/
Publicfunctionset_ip ($ ip = ''){
If (! Empty ($ ip )){
Curl_setopt ($ this-> ch, CURLOPT_HTTPHEADER, array ("X-FORWARDED-FOR: $ ip", "CLIENT-IP: $ ip "));
}
Return $ ip;
}
} |