php 卷曲,跟随位置但只获取新位置的标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6129000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:25:28  来源:igfitidea点击:

Curl, follow location but only get header of the new location?

phpcurl

提问by Richard Knop

I know that when I set CURLOPT_FOLLOWLOCATION to true, cURL will follow the Location header and redirect to new page. But is it possible only to get header of the new page without actually redirecting there? Or is it not possible?

我知道当我将 CURLOPT_FOLLOWLOCATION 设置为 true 时,cURL 将跟随位置标头并重定向到新页面。但是是否有可能只获取新页面的标题而不实际重定向到那里?或者不可能?

回答by Nathan Dunn

Appears to be a duplicate of PHP cURL: Get target of redirect, without following it

似乎是PHP cURL的副本:获取重定向的目标,但不遵循它

However, this can be done in 3 easy steps:

但是,这可以通过 3 个简单的步骤完成:

Step 1. Initialise curl

步骤 1. 初始化 curl

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

Step 2. Get the headers for $url

步骤 2. 获取标题 $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. Parse the headers to get the new URL

步骤 3. 解析标头以获取新 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-3 as often as you like.

获得新 URL 后,您可以随时重复步骤 2-3。

回答by Marc B

No. You'd have to disable FOLLOWLOCATION, extract the redirect URL from the response, and then issue a new HEAD request with that URL.

不。您必须禁用FOLLOWLOCATION,从响应中提取重定向 URL,然后使用该 URL 发出新的 HEAD 请求。

回答by Ahmed Soliman

Yes, you can set it to follow the redirect until you get the last location on the header response.

是的,您可以将其设置为跟随重定向,直到获得标头响应的最后一个位置。

The function to get the last redirect:

获取最后一次重定向的函数:

function get_redirect_final_target($url)
{
    $ch = curl_init($url);        
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow redirects
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // set referer on redirect
    curl_setopt($ch,CURLOPT_HEADER,false); // if you want to print the header response change false to true
    $response = curl_exec($ch);
    $target = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    curl_close($ch);
    if ($target)
        return $target; // the location you want
    return false;
}

回答by fvox

Set CURLOPT_FOLLOWLOCATIONas falseand CURLOPT_HEADERas true, and get the "Location" from the response header.

设置CURLOPT_FOLLOWLOCATIONasfalseCURLOPT_HEADERas true,并从响应头中获取“位置”。

回答by azat

And for analyze headers, your can use CURLOPT_HEADERFUNCTION

对于分析标题,您可以使用CURLOPT_HEADERFUNCTION

回答by shacharsol

Make sure you set CURLOPT_HEADERto True to get the headers in the response, otherwise the response returned as blank string

确保设置CURLOPT_HEADER为 True 以获取响应中的标头,否则响应以空字符串形式返回

回答by ctrlbrk

You can get the redirect URL directly with curl_getinfo:

可以通过 curl_getinfo 直接获取重定向 URL:

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_COOKIESESSION, false);
  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
  $redirect = curl_getinfo($ch)['redirect_url'];
  curl_close($ch);

  return $redirect;