In-depth analysis of php curl

Source: Internet
Author: User

Curl is a very powerful function in php. Every php programmer should learn and be familiar with curl. Make sure that your php_curl extension is enabled before using curl.

I. curl usage
For example, we collected the first page of PHP recruitment for Shenzhen Zhaopin.Copy codeThe 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); // The content is not automatically output.
Curl_setopt ($ ch, CURLOPT_HEADER, 0); // No header information is returned.
// Execute curl
$ Output = curl_exec ($ ch );
// Error message
If (curl_exec ($ ch) === false ){
Die (curl_error ($ ch ));
}
// Release the curl handle
Curl_close ($ ch );
Header ('content-type: text/html; charset = UTF-8 ');
Echo $ output;

Of course, we must use <Regular Expression> to process the returned data, find the part we want, and then fill the data in your website according to your needs.Copy codeThe Code is as follows: // job name
Preg_match_all ('/<td class = "Jobname"> .*? <A \ s * href = "(.*?) "\ Starget =" _ blank "> (.*?) <\/A>/s', $ output, $ title );
$ Title [1]; // link
$ Title [2]; // title
// Company name
Preg_match_all ('/<td class = "Companyname"> .*? <A href = "(.*?) "\ Starget =" _ blank "> (.*?) <\/A>/s', $ output, $ company );
$ Company [1]; // link
$ Company [2]; // name
// Work location
Preg_match_all ('/<td class = "Companyaddress"> \ s *(.*?) \ S * <\/td>/s', $ output, $ address );
$ Address [1]; // location
// Release date
Preg_match_all ('/<td class = "releasetime"> \ s *(.*?) \ S * <\/td>/s', $ output, $ time );
$ Time [1]; // time
Var_dump ($ time [1]);

Ii. Common functions
The core of curl is to set various options to achieve various functions. Here we will introduce several common options.
1. post Data
Copy codeThe Code is as follows: $ post = array (
'Uid' => 'test ',
'Pwd' => 'curl123'
);
Curl_setopt ($ ch, CURLOPT_POST, 1); // set it to POST
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ post); // POST Data

2. cookie
Copy codeThe Code is as follows: inclusavefile=dirname({file=}.'save.txt ';
When getfile1_dirname({file=}.'get.txt ';
// Available for separate use
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ savefile); // save
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ getfile); // read

3. Counterfeit IP addresses and routes
Copy codeCode: curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ('x-FORWARDED-FOR: 8.8.8.8 ', 'client-IP: 8.8.8.8'); // construct an IP address
Curl_setopt ($ ch, CURLOPT_REFERER, "http://www.baidu.com"); // construct a path

Curl_setopt option Daquan, see the PHP Manual: http://www.php.net/manual/zh/function.curl-setopt.php
3. Multithreading
Official exampleCopy codeThe Code is as follows: // create a cURL Resource
$ Response = curl_init ();
$ Ch2 = curl_init ();
// Set the URL and corresponding options
Curl_setopt ($ scheme, CURLOPT_URL, "http://www.example.com /");
Curl_setopt ($ scheme, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch2, CURLOPT_URL, "http://www.php.net /");
Curl_setopt ($ ch2, CURLOPT_HEADER, 0 );
// Create a cURL handle for Batch Processing
$ Mh = curl_multi_init ();
// Add two handles
Curl_multi_add_handle ($ mh, $ handle );
Curl_multi_add_handle ($ mh, $ ch2 );
$ Running = null;
// Execute the batch processing handle
Do {
Usleep (10000 );
Curl_multi_exec ($ mh, $ running );
} While ($ running> 0 );
// Close all handles
Curl_multi_remove_handle ($ mh, $ handle );
Curl_multi_remove_handle ($ mh, $ ch2 );
Curl_multi_close ($ mh );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.