PHP Curl Request information and return information Setup code instance, curl Instance _php tutorial

Source: Internet
Author: User

PHP Curl Request information and return information set code instance, curl instance


When crawling Web content with curl, it is often necessary to know that the request header information returned by the Web page, and the relevant information about the request, especially when there is a redirect in the request process to obtain the request return header information is very helpful for analyzing the request content

Here is an example of a redirect in the request, and our goal is to get the URL address of the final actual request

$url = ' http://www.appchina.com/market/r/489267/com.appshare.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 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 for redirection curl_setopt ($ch, curlopt_returntransfer,1);//not directly output response Curl_ Setopt ($ch, curlopt_followlocation,1);//If there is a location value in the returned response header, the request $content =curl_exec ($ch) is recursive; $rinfo =curl_getinfo ($ch);  Echo $content, "

Below is the result of the output

http/1.1 OKServer:nginxDate:Sat, 06:17:44 gmtcontent-type:application/  Vnd.android.package-archiveconnection:closelast-modified:mon, 16:00:00 gmtexpires:tue, 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.1529  [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 ())  

As can be seen, after the recursive request to finally get a 200 response, but this method can not get the last request URL, that is the URL of the final actual request, to get this URL requires a recursive analysis of each request returned response

Here's what I wrote. A recursive function that gets the URL of the last request

$url = ' http://www.appchina.com/market/r/489267/com.appshare.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 Curl execution time not to exceed 3 seconds //curl_setopt ($ch, Curlopt_nobody, 1);//This line can not be, if added, then when encountering 302 redirects will not get the real request URL curl_setopt ($ch, curlopt_returntransfer,1); $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); }

http://www.bkjia.com/PHPjc/990544.html www.bkjia.com true http://www.bkjia.com/PHPjc/990544.html techarticle PHP Curl Request information and return information set code instance, curl instance when crawling Web content with curl, often need to know, the page return request header information, and request related information ...

  • 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.