Curl can be said to be a very powerful function in PHP. Every PHP programmer should learn and be familiar with curl. Make sure your php_curl extension is turned on before using curl.
1. Using curl
For example: We collect the first page information of PHP recruitment on Shenzhen Zhilian Recruitment
> to process the returned data, find out the part we want, and then fill the data into your website according to your needs
Copy code The code is as follows:
//Job name
preg_match_all('/
(.*?)/s', $output, $title); $title [1];//Link $title[2];//Title //Company name preg_match_all('/ | .*?(.*?)/s', $output, $company); $company[1];//Link $company[2];//Name //Working location preg_match_all('/ | s*(.*?)s* | /s ', $output, $address);
$address[1];//Place
//Release date
preg_match_all('/
s*(.* ?)s* | /s', $output, $time);
$time[1];//Time
var_dump($time[1]);
2. Common functions
The core of curl is to achieve various functions by setting various options. Here we introduce several commonly used options.
1.post data
Copy code The code is as follows:
$post=array(
'uid'=>'test',
'pwd'=>'curl123'
);
curl_setopt($ch, CURLOPT_POST, 1);//Set to POST mode
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));//POST data
2. cookie
Copy the code The code is as follows:
$savefile=dirname(__FILE__).'save.txt';
$getfile=dirname(__FILE__).'get.txt' ;
//Can be used separately
curl_setopt($ch, CURLOPT_COOKIEJAR, $savefile); //Save
curl_setopt($ch, CURLOPT_COOKIEFILE, $getfile); //Read
3. Forged IP and source
Copy code The code is as follows:
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:8.8.8.8', 'CLIENT-IP:8.8.8.8'));//Construct IP
curl_setopt($ch, CURLOPT_REFERER, "http:// www.baidu.com");//The origin of the structure
curl_setopt options, see the PHP manual for details: http://www.php.net/manual/zh/function.curl-setopt .php
3. Multi-threading
Official example
Copy code The code is as follows:
// Create a pair of cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// Set the URL and 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 handles
$mh = curl_multi_init();
// Add 2 handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$running=null;
// Execute 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);
http://www.bkjia.com/PHPjc/327097.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327097.htmlTechArticlecurl can be said to be a very powerful function in php. Every php programmer should learn and be familiar with curl. Make sure your php_curl extension is enabled before using curl. 1. Using curl For example: I...