gpt4 book ai didi

php - 尽管有 FOLLOWLOCATION,但使用 cURL 获取 301

转载 作者:行者123 更新时间:2023-12-05 08:01:27 24 4
gpt4 key购买 nike

尽管使用了 FOLLOWLOCATION 和 MAXREDIRS,我还是收到了 301 错误。我不知道该怎么做,我尽我所能:将 HEADER 设置为 0,将 FOLLOWLOCATION 设置为 1,将 MAXREDIRS 设置为 30,多次更改 USERAGENT,单独使用 COOKIEFILE,然后使用 COOKIEJAR,但没有任何效果。

这是最奇怪的部分:我试图抓取的同一个网站没有为其他页面提供 301,只是为某些页面提供。有什么想法吗??

function curl_start($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.4");
curl_setopt($ch, CURLOPT_REFERER, "http://google.com/");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}

最佳答案

除非您在安全模式下运行 php,否则它应该可以工作。但即便如此,您的情况也不是问题。

无论如何,试试这个。

<?php
function curl_redirect_exec($ch, &$redirects, $curlopt_header = false) {
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
list($header) = explode("\r\n\r\n", $data, 2);
$matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
$url = trim(array_pop($matches));
$url_parsed = parse_url($url);
if (isset($url_parsed)) {
curl_setopt($ch, CURLOPT_URL, $url);
$redirects++;
return curl_redirect_exec($ch, $redirects);
}
}
if ($curlopt_header)
return $data;
else {
list(,$body) = explode("\r\n\r\n", $data, 2);
return $body;
}
}
?>

源代码:http://www.php.net/manual/en/function.curl-setopt.php#95027

关于php - 尽管有 FOLLOWLOCATION,但使用 cURL 获取 301,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14054652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com