Case Study of the php curl method curl_setopt () function (crawling web pages and POST data), curlcurl_setopt

Source: Internet
Author: User

Case Study of the php curl method curl_setopt () function (crawling web pages and POST data), curlcurl_setopt

Using the curl_setopt () function, you can easily and quickly capture webpages (which makes it easy to collect and laugh). curl_setopt isPHPAn extension library

Usage condition: You need to enable the configuration in php. ini. (PHP 4> = 4.0.2)
// Cancel the comment below

extension=php_curl.dll

InLinuxNext, you need to re-compile PHP. during compilation, you need to open the compilation parameter -- add the "-with-curl" parameter to the configure command.

1. A simple case for capturing web pages:

[Php] view plain copy print? // Create a new cURL resource $ ch = curl_init (); // set the URL and the corresponding options curl_setopt ($ ch, CURLOPT_URL, "http://www.baidu.com/"); curl_setopt ($ ch, CURLOPT_HEADER, false); // capture the URL and pass it to the browser curl_exec ($ ch); // close the cURL resource and release the system resource curl_close ($ ch );

2. POST Data case:

[Php] view plain copy print? // Create a new cURL resource $ ch = curl_init (); $ data = 'phone = '. urlencode ($ phone); // set the URL and the corresponding options curl_setopt ($ ch, CURLOPT_URL, "http://www.post.com/"); curl_setopt ($ ch, CURLOPT_POST, 1 ); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // capture the URL and pass it to the browser curl_exec ($ ch); // close the cURL resource, and release the system resource curl_close ($ ch );

3. About SSL and Cookie

For SSL, that is, the HTTPS protocol, you only need to change the http: // In the CURLOPT_URL connection to https. Of course, the parameter CURLOPT_SSL_VERIFYHOST can be set as a verification site.

For Cookie, you need to understand the following three parameters:

  • CURLOPT_COOKIE: Set a cookie in the face-to-face session.
  • CURLOPT_COOKIEJAR saves a Cookie when the session ends.
  • CURLOPT_COOKIEFILE: the Cookie file.

PS: Part of the Sina Weibo login API is intercepted (I have added some comments and translated all the parameters. Haha) if you are interested in your own research, you can use it yourself. Hey

[Php] view plain copy print? /*** Make an HTTP request ** @ return string API results * @ ignore */function http ($ url, $ method, $ postfields = NULL, $ headers = array () {$ this-> http_info = array (); $ ci = curl_init ();/* Curl settings */curl_setopt ($ ci, CURLOPT_HTTP_VERSION, curl_http_version_00000); // Let the cURL determine the version of curl_setopt ($ ci, CURLOPT_USERAGENT, $ this-> useragent) by itself; // contains a "User-Agent" in the HTTP request: "header string. Curl_setopt ($ ci, CURLOPT_CONNECTTIMEOUT, $ this-> connecttimeout); // the waiting time before the connection is initiated. If it is set to 0, the curl_setopt ($ ci, CURLOPT_TIMEOUT, $ this-> timeout); // sets the maximum number of seconds that cURL can be executed curl_setopt ($ ci, CURLOPT_RETURNTRANSFER, TRUE); // returns the native (Raw) Output curl_setopt ($ ci, CURLOPT_ENCODING, ""); // the value of "Accept-Encoding:" in the HTTP request header. Supported encodings include "identity", "deflate", and "gzip ". If it is an empty string "", the request header will send all supported encoding types. Curl_setopt ($ ci, CURLOPT_SSL_VERIFYPEER, $ this-> ssl_verifypeer); // After the cURL is disabled, the service end verifies curl_setopt ($ ci, CURLOPT_HEADERFUNCTION, array ($ this, 'gethead'); // The first is the resource handle of cURL, and the second is the output header data curl_setopt ($ ci, CURLOPT_HEADER, FALSE ); // When enabled, the header file information will be output as a data stream switch ($ method) {case 'post': curl_setopt ($ ci, CURLOPT_POST, TRUE); if (! Empty ($ postfields) {curl_setopt ($ ci, CURLOPT_POSTFIELDS, $ postfields); $ this-> postdata = $ postfields;} break; case 'delete': curl_setopt ($ ci, CURLOPT_CUSTOMREQUEST, 'delete'); if (! Empty ($ postfields) {$ url = "{$ url }? {$ Postfields} ";}} if (isset ($ this-> access_token) & $ this-> access_token) $ headers [] =" Authorization: oau22 ". $ this-> access_token; $ headers [] = "API-RemoteIP :". $ _ SERVER ['remote _ ADDR ']; curl_setopt ($ ci, CURLOPT_URL, $ url); curl_setopt ($ ci, CURLOPT_HTTPHEADER, $ headers); curl_setopt ($ ci, CURLINFO_HEADER_OUT, TRUE); $ response = curl_exec ($ ci); $ this-> http_code = curl_getinfo ($ ci, CURLINFO_HTTP_CODE); $ this-> http_info = array_merge ($ this-> http_info, curl_getinfo ($ ci); $ this-> url = $ url; if ($ this-> debug) {echo "==== post data =====\ r \ n"; var_dump ($ postfields ); echo '= info = '. "\ r \ n"; print_r (curl_getinfo ($ ci); echo '====$ response ==== '. "\ r \ n"; print_r ($ response);} curl_close ($ ci); return $ response ;}

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.