php curl:我如何模拟一个获取请求完全像一个web浏览器? [英] php curl: how can i emulate a get request exactly like a web browser?

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

问题描述

有网站,当我打开特定的ajax请求浏览器,我得到的结果页面,但是当我尝试加载它们与curl,我收到一个错误从服务器。


$ b $



这是我在做什么:

$ b $ p b

  $ 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 ',...)?有一个选项CURLOPT_USERAGENT在 http://docs.php.net/function.curl-setopt

是否还有服务器测试的Cookie?您可以使用CURLOPT_COOKIE,CURLOPT_COOKIEFILE和/或CURLOPT_COOKIEJAR来处理。



编辑:由于请求使用https,因此在验证证书时可能会出错,请参阅CURLOPT_SSL_VERIFYPEER。 / p>

  $ 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);


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 ?

that's what i'm doing:

$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;

解决方案

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.

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

这篇关于php curl:我如何模拟一个获取请求完全像一个web浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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