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
$url='http://sou.zhaopin.com/jobs/searchresult.ashx?jl=%E6%B7%B1%E5%9C%B3&kw=php&sm=0&p=1';
// 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){
die(curl_error($ch));
}
// The release of curl handle
curl_close($ch);
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
var_dump($time[1]);
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
$post=array(
'uid'=>'test',
'pwd'=>'curl123'
);
curl_setopt($ch, CURLOPT_POST, 1);// Set to POST way
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));//POST data
2.cookie
$savefile=dirname(__FILE__).'save.txt';
$getfile=dirname(__FILE__).'get.txt';
// 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:8.8.8.8', 'CLIENT-IP:8.8.8.8'));// structure IP
curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com");// 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, "http://www.example.com/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
// Create batch cURL handle
$mh = curl_multi_init();
// increase 2 A handle
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$running=null;
// Execute the batch handle
do {
usleep(10000);
curl_multi_exec($mh,$running);
} while ($running > 0);
// Close all handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);