PHP - 在跟随重定向时我应该设置 cURL 'AUTOREFERER' 吗?
PHP - cURL should I set 'AUTOREFERER' when following redirects?
TL;DR
为什么 应该 或 不应该 我在我的 cURL 函数中设置 CURLOPT_AUTOREFERER => true
(如下重定向数量有限)?
加长版
我有一个非常标准的 cURL 函数,return headers 对于给定的 URL,遵循 up-to 10 次重定向...
const SINGLETIMEOUT = 8; // Seconds (is this too long?)
public static function getHeaders($url, $userAgent) {
// Initialize cURL object
$curl = curl_init($url);
// Set options
curl_setopt_array($curl, array(
CURLOPT_USERAGENT => $userAgent,
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_AUTOREFERER => true,
CURLOPT_TIMEOUT => SINGLETIMEOUT, // 5 seconds (safety!)
CURLOPT_CONNECTTIMEOUT => SINGLETIMEOUT
));
// Run it
curl_exec($curl);
// Get headers
$headers = curl_getinfo($curl);
// Close it
curl_close($curl);
return $headers;
}
getHeaders
函数运行良好,完全符合预期。但到目前为止,在我的测试中,无论是否包含 CURLOPT_AUTOREFERER => true
,性能或结果都没有差异。有很多参考资料说明 CURLOPT_AUTOREFERER
做了什么 ,但除此之外我找不到任何更深入的内容选项。
好的,所以设置 `` 将
... automatically set the Referer: header field in HTTP requests where it follows a Location: redirect
那又怎样?为什么这很重要?我应该把它留在里面还是扔掉?它会导致某些 URL 的结果不同吗?某些域 return 会不会出错 headers,就像我发送空用户代理时一样?
等等……
我发现的大多数 示例都没有包含它 - 但它们也没有包含我包含的许多其他选项。
首先确定一些基本信息:根据维基百科:
The HTTP referer (originally a misspelling of referrer) is an HTTP header field that identifies the address of the webpage (i.e. the URI or IRI) that linked to the resource being requested. By checking the referrer, the new webpage can see where the request originated.
In the most common situation this means that when a user clicks a hyperlink in a web browser, the browser sends a request to the server holding the destination webpage. The request includes the referer field, which indicates the last page the user was on (the one where they clicked the link).
Referer logging is used to allow websites and web servers to identify where people are visiting them from, for promotional or statistical purposes.
但是这里有一个重要的细节。这个header是客户端提供的,客户端可以选择提供,也可以选择不提供。此外,如果客户端选择提供它,那么客户端可以提供它想要的任何值。
正因为如此,开发人员已经学会了不要真正依赖他们获得的引荐来源价值,而不是统计数据,因为它很容易被欺骗(实际上你可以在 cURL 中自己设置引荐来源 header如果你想打电话而不是使用 CURLOPT_AUTOREFERER
).
因此,在使用爬虫或 cURL 时提供它通常是无关紧要的。是否想让远程站点知道您来自哪里,这取决于您。它应该仍然有效。
也就是说,网站并非不可能根据引荐来源网址显示不同的结果,例如,我看到一个网站正在检查引荐来源网址是否 Google 以提供额外的 in-site 搜索结果,但这是例外情况而不是规则,除此之外网站应该始终可用。
TL;DR
为什么 应该 或 不应该 我在我的 cURL 函数中设置 CURLOPT_AUTOREFERER => true
(如下重定向数量有限)?
加长版
我有一个非常标准的 cURL 函数,return headers 对于给定的 URL,遵循 up-to 10 次重定向...
const SINGLETIMEOUT = 8; // Seconds (is this too long?)
public static function getHeaders($url, $userAgent) {
// Initialize cURL object
$curl = curl_init($url);
// Set options
curl_setopt_array($curl, array(
CURLOPT_USERAGENT => $userAgent,
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_AUTOREFERER => true,
CURLOPT_TIMEOUT => SINGLETIMEOUT, // 5 seconds (safety!)
CURLOPT_CONNECTTIMEOUT => SINGLETIMEOUT
));
// Run it
curl_exec($curl);
// Get headers
$headers = curl_getinfo($curl);
// Close it
curl_close($curl);
return $headers;
}
getHeaders
函数运行良好,完全符合预期。但到目前为止,在我的测试中,无论是否包含 CURLOPT_AUTOREFERER => true
,性能或结果都没有差异。有很多参考资料说明 CURLOPT_AUTOREFERER
做了什么 ,但除此之外我找不到任何更深入的内容选项。
好的,所以设置 `` 将
... automatically set the Referer: header field in HTTP requests where it follows a Location: redirect
那又怎样?为什么这很重要?我应该把它留在里面还是扔掉?它会导致某些 URL 的结果不同吗?某些域 return 会不会出错 headers,就像我发送空用户代理时一样?
等等……
我发现的大多数 示例都没有包含它 - 但它们也没有包含我包含的许多其他选项。
首先确定一些基本信息:根据维基百科:
The HTTP referer (originally a misspelling of referrer) is an HTTP header field that identifies the address of the webpage (i.e. the URI or IRI) that linked to the resource being requested. By checking the referrer, the new webpage can see where the request originated. In the most common situation this means that when a user clicks a hyperlink in a web browser, the browser sends a request to the server holding the destination webpage. The request includes the referer field, which indicates the last page the user was on (the one where they clicked the link). Referer logging is used to allow websites and web servers to identify where people are visiting them from, for promotional or statistical purposes.
但是这里有一个重要的细节。这个header是客户端提供的,客户端可以选择提供,也可以选择不提供。此外,如果客户端选择提供它,那么客户端可以提供它想要的任何值。
正因为如此,开发人员已经学会了不要真正依赖他们获得的引荐来源价值,而不是统计数据,因为它很容易被欺骗(实际上你可以在 cURL 中自己设置引荐来源 header如果你想打电话而不是使用 CURLOPT_AUTOREFERER
).
因此,在使用爬虫或 cURL 时提供它通常是无关紧要的。是否想让远程站点知道您来自哪里,这取决于您。它应该仍然有效。
也就是说,网站并非不可能根据引荐来源网址显示不同的结果,例如,我看到一个网站正在检查引荐来源网址是否 Google 以提供额外的 in-site 搜索结果,但这是例外情况而不是规则,除此之外网站应该始终可用。