您好,欢迎访问一九零五行业门户网

php curl请求信息和返回信息设置代码实例_PHP教程

php curl请求信息和返回信息设置代码实例 这篇文章主要介绍了php curl请求信息和返回信息设置代码实例,本文直接给出代码实例,需要的朋友可以参考下
在用curl抓取网页内容的时候,经常要知道,网页返回的请求头信息,和请求的相关信息,特别是在请求过程中存在重定向的时候获取请求返回头信息对分析请求内容很有帮助
下面就是一个请求中存在重定向的例子,我们的目的是要获取最终实际请求的url地址
?
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
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 ( ))
可以看到,经过递归请求后最终得到一个200的response,但是这中方式不能得到最后一次请求的url,也就是最终实际请求的url,要想得到这个url就需要递归的分析每次请求返回的response
下面是我写的一个获取最后一次请求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;
}
http://www.bkjia.com/phpjc/990989.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/990989.htmltecharticlephp curl请求信息和返回信息设置代码实例 这篇文章主要介绍了php curl请求信息和返回信息设置代码实例,本文直接给出代码实例,需要的朋友可...
其它类似信息

推荐信息