我会尝试使用PHP的cURL在RedHat Linux服务器上访问互联网位置来诊断问题。
cURL已安装并正常工作,且:
<?php var_dump(curl_version()); ?>
输出显示了所有正确的信息。问题在于我可以使用PHP通过cURL连接到本地主机,但无法连接到互联网(见下文)。
通常我会怀疑防火墙,但是我可以从命令行连接到互联网而没有任何问题。该主机也可以更新自己的软件包等。
我错过了什么?我的测试如下:
<?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'));
?>