In-depth understanding of the curl class, which can be used to simulate get, post, and curl download _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
A deep understanding of the curl class can be used to simulate get, post, and curl downloads. The copy code is as follows :? PhpclassCurl {** get method to obtain the access address * @ paramstringurl the address to be accessed * @ paramstringcookiecookie storage address As follows:

The code is as follows:


Class Curl {
/*
* Get access to the specified address
* @ Param string url the address to be accessed
* @ Param string cookie: The storage address of the cookie. If no cookie exists, no cookie is sent.
* @ Return string curl_exec () information obtained
* @ Author andy
**/
Public function get ($ url, $ cookie = '')
{
// Initialize a cURL session
$ Curl = curl_init ($ url );
// No header information is displayed.
Curl_setopt ($ curl, CURLOPT_HEADER, 0 );
// Return the information obtained by curl_exec () in the form of a file stream instead of directly outputting it.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
// Use automatic redirect
Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1 );
If (! Empty ($ cookie )){
// The name of the file containing the cookie data. the format of the cookie file can be Netscape, or the file is saved only with HTTP header information.
Curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ cookie );
}
// Automatically set Referer
Curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1 );
// Execute a curl session
$ Tmp = curl_exec ($ curl );
// Close the curl session
Curl_close ($ curl );
Return $ tmp;
}
/*
* Simulate the specified address of a request in post mode
* @ Param string the specified address of the url request
* @ Param array
* # Patam string cookie storage address
* @ Return string curl_exec () information obtained
* @ Author andy
**/
Public function post ($ url, $ params, $ cookie)
{
$ Curl = curl_init ($ url );
Curl_setopt ($ curl, CURLOPT_HEADER, 0 );
// Check the certificate Source. 0 indicates that the certificate validity check is blocked.
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, false );
// Check whether the SSL encryption algorithm exists from the certificate
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1 );
// Simulate the browser used by the user. the HTTP request contains a string containing the "user-agent" header.
Curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['http _ USER_AGENT ']);
// Send a regular POST request in the type of application/x-www-form-urlencoded, just like submitting a form.
Curl_setopt ($ curl, CURLOPT_POST, 1 );
// Return the information obtained by curl_exec () in the form of a file stream instead of directly outputting it.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
// Use automatic redirect
Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1 );
// Automatically set Referer
Curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1 );
// Cookie address
Curl_setopt ($ curl, CURLOPT_COOKIEJAR, $ cookie );
// All data is sent using the "POST" operation in the HTTP protocol. To send a file,
// Add the @ prefix to the file name and use the full path. This parameter can be passed through the urlencoded string
// Similar to 'para1 = val1 limit 2 = val2 &... 'or use an array with the field name as the key value and field data as the value
// If the value is an array, the Content-Type header is set to multipart/form-data.
Curl_setopt ($ curl, CURLOPT_POSTFIELDS, http_build_query ($ params ));
$ Result = curl_exec ($ curl );
Curl_close ($ curl );
Return $ result;
}
/**
* Remote download
* @ Param string $ remote Image address
* @ Param string $ address saved locally
* @ Param string $ optional cookie address parameter
* In some websites, cookies are required to download images on the website.
* Therefore, you need to add cookies.
* @ Return void
* @ Author andy
*/
Public function reutersload ($ remote, $ local, $ cookie = ''){
$ Cp = curl_init ($ remote );
$ Fp = fopen ($ local, "w ");
Curl_setopt ($ cp, CURLOPT_FILE, $ fp );
Curl_setopt ($ cp, CURLOPT_HEADER, 0 );
If ($ cookie! = ''){
Curl_setopt ($ cp, CURLOPT_COOKIEFILE, $ cookie );
}
Curl_exec ($ cp );
Curl_close ($ cp );
Fclose ($ fp );
}
}

The pipeline code is as follows :? Php class Curl {/** get method to obtain the address specified for access * @ param string url the address to be accessed * @ param string cookie storage address...

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.