1

我正在尝试执行一个 cURL 请求(直接来自 shell 或通过 PHP),它将返回与通过浏览器发出的请求基本相同的 URL 内容(减去任何 cookie/登录等)。

对www.google.com的基本 cURL 请求将返回似乎是带有一些字符编码问题的日文版 Google 搜索。

使用包括设置标准用户代理和关注位置在内的选项进行测试仍然不会导致我认为对我的浏览器非常相似的请求。我应该使用一组标志来密切模仿浏览器请求吗?

下面的代码目前用于测试,但即使存储了 cookie,Google 仍假定位置是日本 (google.co.jp)。

$header = array(
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language: en-us,en;q=0.5",
        "Connection: keep-alive",
        "Cache-Control: no-cache",
        "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
        "Pragma: no-cache",
    );
$useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);
4

1 回答 1

2
$header = array(
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language: en-us,en;q=0.5",
        "Connection: keep-alive",
        "Cache-Control: no-cache",
        "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
        "Pragma: no-cache",
    );
$useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_PROXY, 'PROXY_IP_HERE:PROXY_PORT'); // Use a proxy located in USA
$data = curl_exec($ch);
curl_close($ch);
于 2013-01-02T01:23:08.937 回答