An in depth analysis of php curl

  • 2020-06-03 06:08:46
  • OfStack

curl is one of the most powerful features in php. Every php programmer should learn and become familiar with curl. Make sure your php_curl extension is enabled before using curl.

1. curl use
For example, we collected the information on page 1 of PHP recruitment in Zhaopin shenzhen

// Initialize the 
$ch = curl_init();
// Set options, including URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// Does not automatically output content 
curl_setopt($ch, CURLOPT_HEADER, 0);// No header information is returned 
// perform curl
$output = curl_exec($ch);
// Error message 
if(curl_exec($ch) === false){
// The release of curl handle 
header('Content-type: text/html; charset=utf-8');
echo $output;

Of course we have to use the returned data < < Regular expression > > Process, figure out which section we want, and then populate your site with the data you need

// Job title 
preg_match_all('/<td class="Jobname">.*?<a\s*href="(.*?)"\starget="_blank">(.*?)<\/a>/s', $output, $title);
$title[1];// link 
$title[2];// The title 
// The name of the company 
preg_match_all('/<td class="Companyname">.*?<a href="(.*?)"\starget="_blank">(.*?)<\/a>/s', $output, $company);
$company[1];// link 
$company[2];// The name 
// Working place 
preg_match_all('/<td class="Companyaddress">\s*(.*?)\s*<\/td>/s', $output, $address);
$address[1];// place 
// The release date 
preg_match_all('/<td class="releasetime">\s*(.*?)\s*<\/td>/s', $output, $time);
$time[1];// time 

2. Common functions
At the heart of curl is the ability to achieve functionality by setting options, and we present several common options here.
1. post data

curl_setopt($ch, CURLOPT_POST, 1);// Set to POST way 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));//POST data 


// You can use them separately 
curl_setopt($ch, CURLOPT_COOKIEJAR, $savefile); // save   
curl_setopt($ch, CURLOPT_COOKIEFILE, $getfile); // read 

3. Forged IP and the way of arrival

curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:', 'CLIENT-IP:'));// structure IP  
curl_setopt($ch, CURLOPT_REFERER, "");// Tectonic background   

curl_setopt options, see the PHP handbook: http: / / www php. net manual/zh/function curl - setopt. php
3. Multithreading
The official sample

//  create 1 right cURL resources 
$ch1 = curl_init();
$ch2 = curl_init();
//  Set up the URL And the corresponding options 
curl_setopt($ch1, CURLOPT_URL, "");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "");
curl_setopt($ch2, CURLOPT_HEADER, 0);
//  Create batch cURL handle 
$mh = curl_multi_init();
//  increase 2 A handle 
//  Execute the batch handle 
do {
} while ($running > 0);
//  Close all handles 
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);

Related articles: