如何像 Web 浏览器一样模拟 get 请求? [英] How can I emulate a get request exactly like a web browser?

查看:42
本文介绍了如何像 Web 浏览器一样模拟 get 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些网站当我在浏览器上打开特定的ajax请求时,我得到了结果页面.但是当我尝试使用 curl 加载它们时,我收到来自服务器的错误.

There are websites that when I open specific ajax request on browser, I get the resulted page. But when I try to load them with curl, I receive an error from the server.

如何正确模拟对将模拟浏览器的服务器的获取请求?

How can I properly emulate a get request to the server that will simulate a browser?

这就是我正在做的:

$url="https://new.aol.com/productsweb/subflows/ScreenNameFlow/AjaxSNAction.do?s=username&f=firstname&l=lastname";
ini_set('user_agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
print $result;

推荐答案

你确定 curl 模块支持 ini_set('user_agent',...) 吗?http://docs.php.net/function.curl-setopt<描述了一个选项 CURLOPT_USERAGENT/a>.
是否也有服务器测试的 cookie?您可以使用 CURLOPT_COOKIE、CURLOPT_COOKIEFILE 和/或 CURLOPT_COOKIEJAR 来处理.

Are you sure the curl module honors ini_set('user_agent',...)? There is an option CURLOPT_USERAGENT described at http://docs.php.net/function.curl-setopt.
Could there also be a cookie tested by the server? That you can handle by using CURLOPT_COOKIE, CURLOPT_COOKIEFILE and/or CURLOPT_COOKIEJAR.

由于请求使用 https,因此验证证书时也可能出错,请参阅 CURLOPT_SSL_VERIFYPEER.

edit: Since the request uses https there might also be error in verifying the certificate, see CURLOPT_SSL_VERIFYPEER.

$url="https://new.aol.com/productsweb/subflows/ScreenNameFlow/AjaxSNAction.do?s=username&f=firstname&l=lastname";
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
var_dump($result);

这篇关于如何像 Web 浏览器一样模拟 get 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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