php 服务器可以阻止 curl 请求吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9391137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can servers block curl requests?
提问by www.amitpatil.me
I am working on ZOHO API and trying to update the record using cURL. I tried different cURL variations, but it always returns "false". But when I call the same URL using a browser, it works.
我正在研究 ZOHO API 并尝试使用 cURL 更新记录。我尝试了不同的 cURL 变体,但它总是返回“false”。但是当我使用浏览器调用相同的 URL 时,它可以工作。
Is there any way they can block cURL requests? Is there any other way I can call that URL using a POST or maybe a GET request?
他们有什么办法可以阻止 cURL 请求?有没有其他方法可以使用 POST 或 GET 请求调用该 URL?
The cURL code I have tried is as below:
我试过的 cURL 代码如下:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
采纳答案by Jon
Servers cannot block cURL requests per se, but they can block any request that they do not like. If the server checks for some parameters that your cURL request does not satisfy, it could decide to respond differently.
服务器本身无法阻止 cURL 请求,但可以阻止任何他们不喜欢的请求。如果服务器检查您的 cURL 请求不满足的某些参数,它可能会决定做出不同的响应。
In the vast majority of cases, this difference in behavior is triggered by the presence (or absence) and values of the HTTP request headers. For example, the server might check that the User-Agent
header is present and has a valid value (it could also check lots of other things).
在绝大多数情况下,这种行为差异是由 HTTP 请求标头的存在(或不存在)和值触发的。例如,服务器可能会检查User-Agent
标头是否存在并具有有效值(它还可以检查许多其他内容)。
To find out what the HTTP request coming from the browser looks like, use an HTTP debugging proxy like Fiddleror your browser's developer tools.
要了解来自浏览器的 HTTP 请求是什么样的,请使用Fiddler 之类的 HTTP 调试代理或浏览器的开发人员工具。
To add your own headers to your cURL request, use
要将您自己的标头添加到您的 cURL 请求中,请使用
curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName: HeaderValue'));
回答by Tom Desp
Many web servers want to block HTTP requests forged by something else than a browser, to prevent bots abuses. If you want to simulate/pretend your request from a browser, you at least have to:
许多 Web 服务器希望阻止由浏览器以外的其他东西伪造的 HTTP 请求,以防止机器人滥用。如果您想模拟/假装来自浏览器的请求,您至少必须:
Pass the exact same headers than your browsers (use ie Firebugto get them)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
Change the user agent (name of the browser)
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
Enable cookies (for eg redirection and session handling)
curl_setopt ($ch, CURLOPT_COOKIEJAR, $file); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
Add referers
curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com'); curl_setopt($curl, CURLOPT_AUTOREFERER, true);
传递与浏览器完全相同的标头(使用 ie Firebug获取它们)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
更改用户代理(浏览器名称)
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
启用 cookie(例如重定向和会话处理)
curl_setopt ($ch, CURLOPT_COOKIEJAR, $file); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
添加推荐人
curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com'); curl_setopt($curl, CURLOPT_AUTOREFERER, true);
And pray you haven't missed anything!
祈祷你没有错过任何东西!
回答by Vitamin
To answer your question "Is there any way they can block CURL requests?": Yes, in fact one may detect a cURL request by reading the User-Agent
header.
回答您的问题“他们有什么办法可以阻止 CURL 请求?” : 是的,实际上可以通过读取User-Agent
标头来检测 cURL 请求。
You can change the user agent by calling curl_setopt($ch, CURLOPT_USERAGENT, 'My user agent string!');
.
您可以通过调用更改用户代理curl_setopt($ch, CURLOPT_USERAGENT, 'My user agent string!');
。
回答by ashton gleason
Just to elaborate a little more on this, you can use the curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); or something like that to fake the user agent. In this case, the server would think a Firefox browser was making the request.
只是为了详细说明这一点,您可以使用 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); 或类似的东西来伪造用户代理。在这种情况下,服务器会认为是 Firefox 浏览器发出请求。