In-depth analysis of php curl_PHP tutorial

WBOY
Release: 2016-07-21 15:10:20
Original
964 people have browsed it

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

Copy code The code is as follows:

$url='http://sou.zhaopin.com/jobs/searchresult.ashx?jl=%E6%B7%B1 %E5%9C%B3&kw=php&sm=0&p=1';
//Initialization
$ch = curl_init();
//Set options, including URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Do not automatically output content
curl_setopt($ch, CURLOPT_HEADER, 0);//Do not return header information
//Execute curl
$output = curl_exec($ch);
//Error message
if(curl_exec($ch) === false){
die(curl_error($ch));
}
//Release curl handle
curl_close($ch);
header('Content-type: text/html; charset=utf-8');
echo $output;

Of course we must use <> 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);

www.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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template