cURL可以工作,但PHP cURL无法连接到internet
尝试在RedHat Linux服务器上使用PHP将问题卷曲到Internet位置来诊断问题 cURL已安装并工作,并且:cURL可以工作,但PHP cURL无法连接到internet,php,url,Php,Url,尝试在RedHat Linux服务器上使用PHP将问题卷曲到Internet位置来诊断问题 cURL已安装并工作,并且: <?php var_dump(curl_version()); ?> 显示输出中的所有正确信息。问题是我可以使用PHP在box本身上卷曲到localhost,但不能在Internet上卷曲到localhost(见下文) 通常我会怀疑防火墙,但我可以毫无问题地从命令行转到Internet。盒子还可以更新自己的软件包等 我错过了什么?我的测试是: <?ph
<?php var_dump(curl_version()); ?>
显示输出中的所有正确信息。问题是我可以使用PHP在box本身上卷曲到localhost,但不能在Internet上卷曲到localhost(见下文)
通常我会怀疑防火墙,但我可以毫无问题地从命令行转到Internet。盒子还可以更新自己的软件包等
我错过了什么?我的测试是:
<?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'));
?>
服务器上是否存在DNS解析问题?它将始终能够解析
localhost
,但可能无法解析www.google.com。试试这个:
var_dump(dns_get_record('www.google.com'));
如果DNS未解析,则应获取:
array(0) {}
如果DNS正在工作,您应该获得如下数组:
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)
}
}
+1纯粹是为了“无法上网”谢谢,但我可以从机器上卷曲“www.google.com”,所以DNS可能没问题。不过我会测试它,也会像其他评论一样直接测试方案和IP。同时检查www数据帐户是否已锁定。已修复。谢谢大家的帮助。我发现这个服务器需要使用代理。我们测试的用户帐户设置了代理,但没有Apache用户。对/etc/init.d/httpd init脚本的编辑处理了这个问题。现在,它提供了一个包含环境变量的文件,所有内容都正常工作。很高兴我能帮忙,哪怕只是一点点。:)