Table of Contents
php curl request information and return information setting code examples
Home Backend Development PHP Tutorial PHP curl request information and return information setting code example_PHP tutorial

PHP curl request information and return information setting code example_PHP tutorial

Jul 13, 2016 am 09:55 AM

php curl request information and return information setting code examples

This article mainly introduces php curl request information and return information setting code examples. This article directly gives code examples. Need Friends can refer to it

When using curl to capture web page content, you often need to know the request header information returned by the web page and the relevant information of the request, especially when there is a redirection during the request process, obtaining the request return header information is helpful for analyzing the request content. Very helpful

The following is an example of a redirection in a request. Our purpose is to obtain the url address of the final actual 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.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);//返回response头部信息

curl_setopt($ch, CURLOPT_NOBODY, 1);//不返回response body内容

//curl_setopt($ch, CURLOPT_MAXREDIRS, 1);//设置请求最多重定向的次数

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//不直接输出response

curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);//如果返回的response 头部中存在Location值,就会递归请求

$content=curl_exec($ch);

$rinfo=curl_getinfo($ch);

 

echo $content,"
";

echo "


";

print_r($rinfo);

1

2

3

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 ( ))

4 5 6 7 8 9 10 11 12 13 14 15 16 17
$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);//Does not return response body content //curl_setopt($ch, CURLOPT_MAXREDIRS, 1);//Set the maximum number of redirect requests curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//Do not output response directly curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);//If there is a Location value in the returned response header, a recursive request will be made $content=curl_exec($ch); $rinfo=curl_getinfo($ch); echo $content,"
"; echo "
"; print_r($rinfo);
The following is the output result  ?
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] =&g t; 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 ( ))

You can see that after a recursive request, you finally get a response of 200, but this method cannot get the URL of the last request, which is the final URL of the actual request. To get this URL, you need to recursively analyze each request. Returned response

The following is a recursive function I wrote to get the last request URL

 ?

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.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);//设置curl执行时间不超过3秒

//curl_setopt($ch, CURLOPT_NOBODY, 1);//这行不能要,如果添上,那么在遇到302重定向的时候就会得不到真正的请求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);

}

return $realUrl;

}

1 2

34 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.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 to no more than 3 seconds //curl_setopt($ch, CURLOPT_NOBODY, 1);//This line is not required. If you add it, you will not get the real request url when encountering a 302 redirect 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); } return $realUrl; }
http://www.bkjia.com/PHPjc/990989.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/990989.htmlTechArticlephp curl request information and return information setting code examples This article mainly introduces php curl request information and return information settings Code examples, this article directly gives code examples, friends who need them can...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles