我如何 CURL www.google.com - 它不断将我重定向到 .co.uk

发布于 2024-08-19 16:51:53 字数 975 浏览 4 评论 0原文

我正在使用 CURL 来检查 URL 是否存在(HEAD 请求),但是当我使用 www.google.com 测试它时,它会将我重定向到 www.google.co.uk< /code> - 可能是因为我的服务器位于英国。

有什么办法可以阻止这种情况发生吗?我不想删除 CURLOPT_FOLLOWLOCATION 选项,因为这对于 301 重定向等很有用。

我的部分代码如下;

$ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);

    $output = curl_exec($ch);

    // get data     
$data = curl_getinfo($ch);

当我将 $url 设置为 www.google.com 时,$data['url'] 包含 www.google.co.uk

I am using CURL to check for the existence of a URL (HEAD request) but when I test it with www.google.com, it redirects me to www.google.co.uk - probably because my server is UK-based.

Is there a way you can stop this from happening? I don't want to remove the CURLOPT_FOLLOWLOCATION option as this is useful for 301 redirects etc.

Part of my code is below;

$ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);

    $output = curl_exec($ch);

    // get data     
$data = curl_getinfo($ch);

$data['url'] contains www.google.co.uk when I set $url as www.google.com

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

独闯女儿国 2024-08-26 16:51:53

您需要将curl 与cookie 一起使用来模拟浏览器中的类似行为。

当您从英国访问 google.com 时,它会将您重定向到 google.co.uk,但是该页面上有一个标题为“转到 google.com”的链接,可让您返回 google.com 并留在那里。它使用 cookie 来记住您的网站偏好。

例如,以下是我执行此操作后得到的 cookie(使用 Firefox):

alt text

You need to use curl with a cookie that simulate a similar behavior in a browser.

When you visit google.com from England it redirects you to google.co.uk, however there is a link on that page titled "go to google.com" that lets you go back to google.com and stay there. It uses a cookie to remember your site preferences.

For example, here are the cookies that I have after doing this (using firefox):

alt text

陌伤浅笑 2024-08-26 16:51:53

尝试访问 www.google.com/ncr,它将避免重定向到 .co.uk(或任何其他国家/地区)页面。

Try accessing www.google.com/ncr, it'll avoid the redirect to the .co.uk (or any other national) page.

迟月 2024-08-26 16:51:53

另一种选择是使用简单的 crypto.google.com。那不会重定向。

Another option is to use simply encrypted.google.com. That won't redirect.

深陷 2024-08-26 16:51:53

有点黑客行为,但是使用 IP 地址怎么样? http://216.239.59.147/ http://66.102.7.104/

A bit of a hack, but how about using an IP address? http://216.239.59.147/ http://66.102.7.104/

冷默言语 2024-08-26 16:51:53

您可以直接使用 www.google.co.uk ,没有区别。 google.com/.net 始终会重定向到您的位置,但如果您使用 .co.uk 等国家/地区 TLD,它不会重定向。

使用 .com 或 .net 时,没有办法(据我所知)阻止重定向。

You could use www.google.co.uk directly, no difference there. google.com/.net always redirect to your location but if you use a country TLD like .co.uk it will not redirect.

There is no way (known to me) to prevent the redirect when using .com or .net.

随风而去 2024-08-26 16:51:53

避免 Google 决定您所在国家/地区的一种方法是设置不同的 IP 地址。只需从网络上获取众多美国代理服务器之一并执行以下操作:

$ch=curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCTION,1); 
curl_setopt($ch,CURLOPT_PROXY,"8.12.33.159");
curl_setopt($ch,CURLOPT_PROXYPORT,"80");
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");
curl_setopt($ch,CURLOPT_URL,$URI);
$results=curl_exec($ch);
curl_close($ch);

这样,Google 就会认为您来自美国 IP 地址,而不是重定向到本地 Google。

One way to avoid Google from deciding what country you are in, is by setting a different IP address. Just get one of the many US proxy servers from the Web and do something like this:

$ch=curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCTION,1); 
curl_setopt($ch,CURLOPT_PROXY,"8.12.33.159");
curl_setopt($ch,CURLOPT_PROXYPORT,"80");
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");
curl_setopt($ch,CURLOPT_URL,$URI);
$results=curl_exec($ch);
curl_close($ch);

This way, Google will think you come form a US IP address and not redirect to a local Google.

七堇年 2024-08-26 16:51:53

你应该关闭curl的跟随位置(将其设置为false),你将不会再被重定向......

   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

You should turn off the follow location from curl (set it to false) and you won't be redirected anymore ...

   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文