Trying to diagnose an issue using PHP to cURL to an Internet location on a RedHat Linux server.
cURL is installed and working, and:
<?php var_dump(curl_version()); ?>
shows all the correct information in the output. The issue is I can use PHP to cURL to localhost on the box itself, but not the Internet (see below).
Normally I'd suspect the firewall, but I can cURL from the command line to the Internet without a problem. The box can also update it's own software packages, etc.
What am I missing? My test is:
<?php
function http_head_curl($url,$timeout=30)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
if ($res === false) {
throw new RuntimeException("cURL exception: ".curl_errno($ch).": ".curl_error($ch));
}
return trim($res);
}
// Succeeds, displaying headers
echo(http_head_curl('localhost'));
// Fails:
echo(http_head_curl('www.google.com'));
?>
Are you having DNS resolution issues on the server? It will always be able to resolve localhost
but may not be able to resolve www.google.com. Try this:
var_dump(dns_get_record('www.google.com'));
If DNS if not resolving, you should get:
array(0) {}
If DNS is working, you should get an array something like this:
array(6) {
[0]=>
array(5) {
["host"]=>
string(14) "www.google.com"
["type"]=>
string(1) "A"
["ip"]=>
string(13) "74.125.201.99"
["class"]=>
string(2) "IN"
["ttl"]=>
int(375)
}
[1]=>
array(5) {
["host"]=>
string(14) "www.google.com"
["type"]=>
string(1) "A"
["ip"]=>
string(14) "74.125.201.105"
["class"]=>
string(2) "IN"
["ttl"]=>
int(375)
}
[2]=>
array(5) {
["host"]=>
string(14) "www.google.com"
["type"]=>
string(1) "A"
["ip"]=>
string(14) "74.125.201.104"
["class"]=>
string(2) "IN"
["ttl"]=>
int(375)
}
[3]=>
array(5) {
["host"]=>
string(14) "www.google.com"
["type"]=>
string(1) "A"
["ip"]=>
string(14) "74.125.201.106"
["class"]=>
string(2) "IN"
["ttl"]=>
int(375)
}
[4]=>
array(5) {
["host"]=>
string(14) "www.google.com"
["type"]=>
string(1) "A"
["ip"]=>
string(14) "74.125.201.103"
["class"]=>
string(2) "IN"
["ttl"]=>
int(375)
}
[5]=>
array(5) {
["host"]=>
string(14) "www.google.com"
["type"]=>
string(1) "A"
["ip"]=>
string(14) "74.125.201.147"
["class"]=>
string(2) "IN"
["ttl"]=>
int(375)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.