PHP curl encapsulation class (including read/write/read/write cookies/post/proxy/counterfeit source IP addresses)

Source: Internet
Author: User
Tags curl set cookie

PHP's curl can implement many very powerful http operations. However, the native method of curl is a little painful, so it encapsulates a class.

Some of the code in this section is sourced from the network. I have added comments and some minor modifications myself. Currently, the following functions are implemented:

Construct post parameters
Read/write/read/write cookie
Counterfeit source IP address
Set proxy

The code is updated occasionally.

The code is as follows: Copy code

<? 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;
 }

}

Friendly reminder: The php curl class is actually to sort the function functions together, and then write a class without any other skills.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.