Php curl request information and return information setting code instance

Source: Internet
Author: User

Php curl request information and return information setting code instance

This article mainly introduces the php curl request information and return information setting code instance. This article provides code instances directly. For more information, see

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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

$ 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 response header information

Curl_setopt ($ ch, CURLOPT_NOBODY, 1); // The response body content is not returned.

// Curl_setopt ($ ch, CURLOPT_MAXREDIRS, 1); // you can specify the maximum number of redirection requests.

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // response is not output directly.

Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // If the returned response header contains a Location Value, the request is recursive.

$ Content = curl_exec ($ ch );

$ Rinfo = curl_getinfo ($ ch );

 

Echo $ content, "</br> ";

Echo "

Print_r ($ rinfo );

The output result is as follows:

?

1

2

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: 2142149

Array ([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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

$ 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 "</br> --->", $ realUrl;

 

Function getRedirectLocation ($ url ){

 

$ RealUrl = $ url;

Echo $ url, "</br> ";

$ 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 cannot be used. If you add this line, you will not get the real request url when you encounter a 302 redirection.

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], "</br> ";

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.