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.
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);
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"
I am returned the expected XML document.
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.
Thanks!
EDIT: 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()
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.
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
I was able to get things working just fine by adding