Why is cURL reporting an invalid IP address as an error?
I have th开发者_如何转开发e following PHP code that uses cURL:
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://area51.stackexchange.com/users/flair/31.json");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$a_data = json_decode(curl_exec($ch));
echo curl_error($ch);
I then get the following error when I try to access the page over HTTP:
Failed to connect to 0.0.0.31: Invalid argument
However, the code works fine when run from the command line.
What could possible cause cURL to try to connect to 0.0.0.31, which is AFAIK, not even a valid IP address?
"What could possible cause cURL to try to connect to 0.0.0.31, which is AFAIK, not even a valid IP address?"
Your DNS is botched. I tested your code and it works.
When you say "the code works fine when run from the command line," do you mean you have run it with the PHP CLI interpreter? If that's the case, you might check for any noticeable mismatches between the output of php -i
vs. phpinfo()
on the web server. It might be some strange version mismatch or environment problem, although your guess is as good as mine.
If you are (as I had originally thought) just talking about running the curl
command, you could try checking version numbers or environment variables there too.
I had the same problem.
I think what you might have done was to construct the url like so:
$url = "http://area51.stackexchange.com/users/flair/" + 31 + ".json";
And for some reason the url then is translated to 0.0.0.31.
Instead try to concatinate the string, like so:
$url = "http://area51.stackexchange.com/users/flair/" . 31 . ".json";
Solved my issue at least!
精彩评论