PHP cURL:获取重定向的目标,而不是跟踪它 [英] PHP cURL: Get target of redirect, without following it

查看:163
本文介绍了PHP cURL:获取重定向的目标,而不是跟踪它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

curl_getinfo函数返回有关HTTP请求结果的大量元数据。但是,由于某种原因,它不包括我想要的信息位,这是目标URL,如果请求返回一个HTTP重定向代码。



I没有使用CURLOPT_FOLLOWLOCATION,因为我想在特殊情况下处理特定的重定向代码。



如果cURL可以跟踪重定向,为什么不能告诉我他们重定向到什么时候它不是跟随他们?



当然,我可以设置CURLOPT_HEADER标志,并选择位置头。

解决方案

这可以通过4个简单的步骤完成:



步骤1.初始化curl

  curl_init($ ch); //初始化curl句柄
// COOKIESESSION是可选的,如果你想保存cookie在内存中使用
curl_setopt($ this-> ch,CURLOPT_COOKIESESSION,true);

步骤2.获取 $ url

  curl_setopt($ ch,CURLOPT_URL,$ url); //指定您的网址
curl_setopt($ ch,CURLOPT_HEADER,true); // include header in http data
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,false); // do not follow redirects
$ http_data = curl_exec($ ch); // hit the $ url
$ curl_info = curl_getinfo($ ch);
$ headers = substr($ http_data,0,$ curl_info ['header_size']); // split out header

步骤3.检查您是否有正确的响应代码

  if(!($ curl_info ['http_code']> 299&& $ curl_info ['http_code']< 309) 
// return,echo,die,whatever you like
return'Error - http code'.curl_info ['http_code']。收到。
}

步骤4.解析标题以获取新网址

  preg_match(!\r\\\
(?:Location | URI):*(。*?)* \r\\\
!,$ headers,$ matches);
$ url = $ matches [1];

一旦你有了新的URL,你可以重复步骤2-4, / p>

The curl_getinfo function returns a lot of metadata about the result of an HTTP request. However, for some reason it doesn't include the bit of information I want at the moment, which is the target URL if the request returns an HTTP redirection code.

I'm not using CURLOPT_FOLLOWLOCATION because I want to handle specific redirect codes as special cases.

If cURL can follow redirects, why can't it tell me what they redirect to when it isn't following them?

Of course, I could set the CURLOPT_HEADER flag and pick out the Location header. But is there a more efficient way?

解决方案

This can be done in 4 easy steps:

Step 1. Initialise curl

curl_init($ch); //initialise the curl handle
//COOKIESESSION is optional, use if you want to keep cookies in memory
curl_setopt($this->ch, CURLOPT_COOKIESESSION, true);

Step 2. Get the headers for $url

curl_setopt($ch, CURLOPT_URL, $url); //specify your URL
curl_setopt($ch, CURLOPT_HEADER, true); //include headers in http data
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); //don't follow redirects
$http_data = curl_exec($ch); //hit the $url
$curl_info = curl_getinfo($ch);
$headers = substr($http_data, 0, $curl_info['header_size']); //split out header

Step 3. Check if you have the correct response code

if (!($curl_info['http_code']>299 && $curl_info['http_code']<309)) {
  //return, echo, die, whatever you like
  return 'Error - http code'.curl_info['http_code'].' received.';
}

Step 4. Parse the headers to get the new URL

preg_match("!\r\n(?:Location|URI): *(.*?) *\r\n!", $headers, $matches);
$url = $matches[1];

Once you have the new URL you can then repeat steps 2-4 as often as you like.

这篇关于PHP cURL:获取重定向的目标,而不是跟踪它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆