我如何CURL www.google.com - 它不断将我重定向到.co.uk [英] How do I CURL www.google.com - it keeps redirecting me to .co.uk

查看:307
本文介绍了我如何CURL www.google.com - 它不断将我重定向到.co.uk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CURL检查是否存在网址(HEAD请求),但是当我使用 www.google.com 测试它时,会将我重定向到 www.google.co.uk - 可能是因为我的伺服器是以英国为基地。



这从发生?我不想删除 CURLOPT_FOLLOWLOCATION 选项,因为这对301重定向等很有用。



我的一部分代码如下;

  $ ch = curl_init(); 

//设置URL和其他适当的选项
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);

//获取数据
$ data = curl_getinfo($ ch);

$ data ['url'] $ url www.google.com时, www.google.co.uk

解决方案

您需要使用curl和在浏览器中模拟类似行为的cookie。 / p>

当您从英国访问google.com时,会将您重定向到google.co.uk,但是该网页上有一个标题为go to google.com的链接您回到google.com并留在那里。



例如,以下是我在执行此操作后使用的cookies(使用firefox):




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

解决方案

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):

这篇关于我如何CURL www.google.com - 它不断将我重定向到.co.uk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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