Deep understanding of the curl class can be used to simulate get,post and curl downloads _php Tips

Source: Internet
Author: User
As shown below:
Copy Code code as follows:

<?php
Class Curl {
/*
* Get way to access specified address
* @param the address to be accessed by the string URL
* @param the address of the string cookie cookie, without sending the cookie
* @return String curl_exec () information obtained
* @author Andy
**/
Public function Get ($url, $cookie = ' ")
{
Initialize a Curl session
$curl = Curl_init ($url);
Do not display header information
curl_setopt ($curl, Curlopt_header, 0);
The information obtained by CURL_EXEC () is returned as a file stream, rather than as a direct output.
curl_setopt ($curl, Curlopt_returntransfer, 1);
Use automatic jump
curl_setopt ($curl, curlopt_followlocation, 1);
if (!empty ($cookie)) {
The file name containing the cookie data, the format of the cookie file can be in Netscape format, or only the pure HTTP header information is saved to the file.
curl_setopt ($curl, Curlopt_cookiefile, $cookie);
}
Automatically set Referer
curl_setopt ($curl, Curlopt_autoreferer, 1);
Perform a Curl session
$tmp = curl_exec ($curl);
Close Curl Session
Curl_close ($curl);
return $tmp;
}
/*
* Post mode simulation request Specify address
* @param the specified address requested by the string URL
* @param array params request with the
* #patam String Cookie Cookie store 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);
A check of the source of the certification certificate, 0 means to prevent the validity of the certificate check.
curl_setopt ($curl, Curlopt_ssl_verifypeer, false);
Check to see if the SSL encryption algorithm exists from the certificate
curl_setopt ($curl, Curlopt_ssl_verifyhost, 1);
Simulates a user-used browser that contains a string of "user-agent" headers in an HTTP request.
curl_setopt ($curl, curlopt_useragent, $_server[' http_user_agent ']);
Send a regular POST request with the type: application/x-www-form-urlencoded, just like the form submission.
curl_setopt ($curl, Curlopt_post, 1);
The information obtained by CURL_EXEC () is returned as a file stream, rather than as a direct output.
curl_setopt ($curl, Curlopt_returntransfer, 1);
Use automatic jump
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" action in the HTTP protocol. To send a file,
Precede the filename with the @ prefix and use the full path. This parameter can be passed after the urlencoded string
Similar to ' Para1=val1¶2=val2&amp ... ' or an array with a field name as the key value, and the field data as a value
If value is an array, the Content-type header will be 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 remote picture address
* @param string $local a locally saved address
* @param string $cookie cookie address optional argument by
* For some websites that require cookies to download pictures on the site
* So 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);
}
}

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.