Deep understanding of the curl class can be used to simulate get post and curl downloads
- 2020-06-12 08:38:07
- OfStack
As shown below:
<?php
class Curl {
/*
* get Method gets access to the specified address
* @param string url The address to visit
* @param string cookie cookie Address of storage , If not, do not send cookie
* @return string curl_exec() Information acquired
* @author andy
**/
public function get( $url, $cookie='' )
{
// Initialize the 1 a cURL The session
$curl = curl_init($url);
// Don't show header information
curl_setopt($curl, CURLOPT_HEADER, 0);
// will curl_exec() The obtained information is returned as a stream of files rather than as direct output.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Use automatic jump
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
if(!empty($cookie)) {
// contains cookie The file name of the data, cookie The format of the file can be Netscape Format, or just plain HTTP The header information is stored in the file.
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
}
// Automatically set Referer
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
// perform 1 a curl The session
$tmp = curl_exec($curl);
// Shut down curl The session
curl_close($curl);
return $tmp;
}
/*
* post Mode impersonates a request to specify an address
* @param string url The specified address of the request
* @param array params That the request carries
* #patam string cookie cookie Store address
* @return string curl_exec() Information acquired
* @author andy
**/
public function post( $url, $params, $cookie )
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, 0);
// The inspection of the source of the certification certificate, 0 Prevents checking the validity of the certificate.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// Check from the certificate SSL Does the encryption algorithm exist
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
// Simulate the user using the browser in HTTP Included in the request 1 A" user-agent The string of the ".
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
// send 1 A regular POST Request of type: application/x-www-form-urlencoded , like a form submission 1 The sample.
curl_setopt($curl, CURLOPT_POST, 1);
// will curl_exec() The obtained information is returned as a stream of files rather than as 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);
// Total data usage HTTP In the agreement "POST" Operation to send. To send the file,
// Prefix the file name @ Prefix and use the full path. This parameter will pass urlencoded After string
// similar 'para1=val1¶2=val2&...' Or use 1 An array with the field name as the key value and the field data as the value
// if value is 1 An array, Content-Type The 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;
}
/**
* The remote download
* @param string $remote Remote image address
* @param string $local Locally saved address
* @param string $cookie cookie address Optional parameters by
* In some sites is required cookie To download pictures from the website
* So we need to add cookie
* @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);
}
}