Set the code instance for phpcurl request information and return information, curl instance _ PHP Tutorial

Source: Internet
Author: User
Set the code instance and curl instance for phpcurl request information and response information. Phpcurl request information and return information setting code instance. when curl is used to capture webpage content, the curl instance often needs to know the request header information returned by the webpage, and request-related information php curl request information and return information set code instance, curl instance

When using curl to capture webpage content, you often need to know the request header information returned by the webpage and the request-related information, it is helpful to analyze the request content, especially when there is redirection in the request process.

The following is an example of redirection in a request. Our purpose is to obtain the url address of the final request.

$ Url = 'http: // www.appchina.com/market/r/489267/com.app#.android.ilisten.vapk? C = aplus. direct & uid = gAJ9cQEu1TlyZxsXN-aB4RaanvFL6t6Bj-vj0rIBs & p = aplus. detail & m = redirect '; $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); // curl_setopt ($ ch, CURLOPT_POST, 1 ); // curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ params); curl_setopt ($ ch, CURLOPT_HEADER, 1); // return the response header information curl_setopt ($ ch, CURLOPT_NOBODY, 1 ); // do not return response body content // curl_setopt ($ ch, CURLOPT_MAXREDIRS, 1); // set the maximum number of requests redirected curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // do not directly output response curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // if the returned response header contains a Location value, it will recursively request $ content = curl_exec ($ ch ); $ rinfo = curl_getinfo ($ ch); echo $ content ,"
"; Echo" "; print_r ($ rinfo );

The output result is as follows:

HTTP/1.1 200 OKServer: nginxDate: Sat, 22 Dec 2012 06:17:44 GMTContent-Type: application/vnd.android.package-archiveConnection: closeLast-Modified: Mon, 03 Dec 2012 16:00:00 GMTExpires: Tue, 03 Dec 2013 16:00:00 GMTCache-Control: max-age=31536000Content-Length: 2142149Array( [url] => http://www.d.appchina.com/McDonald/r/489267/com.appshare.android.ilisten.vapk?c=aplus.direct&uid=gAJ9cQEu1TlyZxsXN-aB4RaanvFL6t6Bj-vj0rIBs&p=aplus.detail&m=redirect [content_type] => application/vnd.android.package-archive [http_code] => 200 [header_size] => 289 [request_size] => 196 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.171621 [namelookup_time] => 0.135256 [connect_time] => 0.152913 [pretransfer_time] => 0.152916 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 2142149 [upload_content_length] => 0 [starttransfer_time] => 0.171582 [redirect_time] => 0 [certinfo] => Array ( )) 

We can see that a 200 response is finally obtained after recursive requests, but this method cannot get the url of the last request, that is, the url of the final request, to get this url, you need to recursively analyze the response returned by each request.

The following is a recursive function that I wrote to obtain the url of the last request.

$ Url = 'http: // www.appchina.com/market/r/489267/com.app#.android.ilisten.vapk? C = aplus. direct & uid = gAJ9cQEu1TlyZxsXN-aB4RaanvFL6t6Bj-vj0rIBs & p = aplus. detail & m = redirect '; [php] view plaincopy $ realUrl = getRedirectLocation ($ url); echo"
---> ", $ RealUrl; function getRedirectLocation ($ url) {$ realUrl = $ url; echo $ url ,"
"; $ Ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_HEADER, 1); curl_setopt ($ ch, CURLOPT_TIMEOUT, 3 ); // Set the curl execution time to no more than 3 seconds // curl_setopt ($ ch, CURLOPT_NOBODY, 1); // This line is not required, in case of 302 redirection, the actual request url curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1) will not be obtained; $ content = curl_exec ($ ch); // echo $ content; $ rinfo = curl_getinfo ($ ch); $ matches = array (); if (preg_match ('/Location: \ s +? (. + ?) \ S +? /', $ Content, $ matches) {// echo $ matches [1],"
"; Unset ($ content); $ realUrl = getRedirectLocation ($ matches [1]);} if (isset ($ content) {unset ($ content );} return $ realUrl ;}

Http://www.bkjia.com/PHPjc/990544.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/990544.htmlTechArticlephp curl request information and return information settings code instance, curl instance in the use of curl capture web content, often need to know, the web page returned request header information, and request related information...

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.