Phpcurl request information and return information setting code example

Source: Internet
Author: User
This article mainly introduces the phpcurl request information and return information setting code example. This article provides code examples. if you need a friend, you can refer to the following when using curl to capture webpage content, the request header information returned by the web page and related information of the request, especially when there is a redirection in the request process to obtain the request response header information is very helpful for analyzing the request content

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 ;}

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.