PHP CURL GET请求返回500,其中bash curl成功 [英] PHP CURL GET request returns 500 where bash curl succeeds
问题描述
我试图使用PHP的CURL向服务器发出GET请求,并且遇到了一些困难。当我通过PHP提出请求时,我从外部服务器返回一个500错误。
I'm attempting to use PHP's CURL to make a GET request to a server and am having some difficulties doing so. When I make the request through PHP I am returned a 500 error from the external server. However, if I make the request using the bash curl, or visit the URL in a browser it succeeds.
我已经剥离了PHP的基本要素:
I've stripped the PHP down to the bare essentials:
$url = 'http://example.com:8080/path/to/service?cmd=my_command&arg=example2.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
print_r(curl_getinfo($ch));
curl_close($ch);
如上所述,这会从example.com返回500错误。但是,如果我执行以下操作:
As stated this returns a 500 error from example.com. However, if I do the following:
[me@host ~] curl "http://example.com:8080/path/to/service?cmd=my_command&arg=example2.com"
XML文档。
什么给了?它必须是URL的编码,如果我把 $ url
var下拉到 http://example.com: 8080
现在,PHP CURL请求响应为200.我尝试用%26 $替换
&
c $ c> - 没有工作(也不会期望它,因为&
在那里的URL是有效的)。我试过做什么的答案为 php curl发送vars使用GET wierd结果,但这也没有帮助。
What gives? It's got to be something with the encoding of the URL, as if I strip the $url
var down to just http://example.com:8080
the PHP CURL request now responds with 200. I've tried replacing the &
with %26
- that didn't work (nor would I expect it to, as &
is valid in the URL there). I've tried doing what the answer for php curl sending vars using GET wierd results suggested, but that didn't help either.
我在这里缺少什么?我相信这是荒谬的简单,但它逃避了我。
What am I missing here? I'm sure that it's something absurdly simple, but it's escaping me.
谢谢!
只是试图在Python中这样做 - 只是为了看到发生了什么 - 它工作正常:
I've just attempted doing this in Python - just to see what happened - and it works fine there:
import urllib2
r = urllib2.urlopen(theURL)
r.read()
推荐答案
事实证明,我正在访问的API需要一个用户代理的所有请求,但没有提供任何信息来表示这种情况。
It turns out that the API I was accessing required a User-Agent for all requests, but did not provide any information to indicate such.
这是常见的事情吗?除了 http:// developer,我找不到其他任何人执行此操作的其他示例.github.com / v3 /#user-agent-required
Is this a common thing? I can't find any other examples of anyone else doing this other than http://developer.github.com/v3/#user-agent-required
我可以通过添加
curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: Some-Agent/1.0");
这篇关于PHP CURL GET请求返回500,其中bash curl成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!