cURL和网页浏览器之间的区别? [英] Difference between cURL and web browser?
问题描述
我试图从以下网址检索网页:
它在我将其粘贴到浏览器时起作用,但当我通过cURL运行它时,出现以下错误消息:< b>请求的url的一个或多个查询字符串参数无效或有意想不到的值,请更正并重试。
它似乎不是如果我提供不同的userAgent或referrer,请发挥作用。有一个重定向,所以我使用CURLOPT_FOLLOWLOCATION。
这是我的代码:
$ ch = curl_init($ page);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_USERAGENT,'Mozilla / 5.0(Windows NT 5.1; rv:12.0)Gecko / 20100101 Firefox / 12.0');
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1);
$ html = curl_exec($ ch);
curl_close($ ch);
echo $ html;
关于为什么像这样的请求可以在浏览器中运行而不是cURL的想法?
问题出在cookie上。这个特定的网站需要设置一个ASP.NET_SessionId cookie来响应。我将以下内容添加到了我的cURL请求中:
curl_setopt($ ch,CURLOPT_COOKIEJAR,'cookie.txt');
curl_setopt($ ch,CURLOPT_COOKIEFILE,'cookie.txt');
curl_setopt($ ch,CURLOPT_COOKIE,'ASP.NET_SessionId = ho1pqwa0nb3ys3441alenm45; path = /; domain = www.medicare.gov');
我不知道是否有任何会话ID可以使用,但它尝试了一些随机数,所有的工作。
I am trying to retrieve a web page from the following url:
It works when I paste it into a browser, but when I run it through cURL, I receive a page with the following error: "One or more query string parameters of requested url are invalid or has unexpected value, please correct and retry."
It doesn't seem to make a difference if I provide a different userAgent or referrer. There is a redirect, so I use CURLOPT_FOLLOWLOCATION.
Here is my code:
$ch = curl_init($page);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$html = curl_exec($ch);
curl_close($ch);
echo $html;
Any thoughts on why a request like this will work in the browser and not with cURL?
The problem was with cookies. This particular site needed an ASP.NET_SessionId cookie set in order to respond. I added the following to my cURL request:
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIE, 'ASP.NET_SessionId=ho1pqwa0nb3ys3441alenm45; path=/; domain=www.medicare.gov');
I don't know if any session id will work, but it tried a couple random ones and they all worked.
这篇关于cURL和网页浏览器之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!