PHP/cURL/Fedora 解析主机名时出现问题,可能是 DNS 问题?

PHP/cURL/Fedora 解析主机名时出现问题,可能是 DNS 问题?

我在具有 PHP 5.3.2、cURL v7.19.7(根据 phpinfo())的 FC12/apache 服务器上针对 cURL/php 有以下简单测试脚本:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/');
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
curl_exec($ch);
if ($error = curl_error($ch)) {
    echo "Error: $error<br />\n";
}
function read_header($ch, $string)
{
    $length = strlen($string);
    echo "Received $length bytes<br />\n";
    return $length;
}

现在,如果我通过浏览器运行它,它会返回:“错误:无法解析主机‘www.google.com’”

如果我通过服务器上的 php-cgi 命令行运行它,它会返回您所期望的内容(不包含所有 BR):

Header: HTTP/1.1 200 OK
Header: Date: Tue, 13 Jul 2010 20:08:09 GMT
Header: Expires: -1
Header: Cache-Control: private, max-age=0
Header: Content-Type: text/html; charset=ISO-8859-1
Header: Server: gws
Header: X-XSS-Protection: 1; mode=block
Header: Transfer-Encoding: chunked
Header:
Received 2507 bytes
Received 1589 bytes
Received 1362 bytes
Received 2734 bytes
Received 1690 bytes

如果我将 google 线路更改为 google 的 ip 例如http://72.14.204.147/然后脚本就可以通过浏览器和命令行运行。

dig、ping、nslookup 和常规 dns 似乎在这台机器上运行良好。我应该补充一点,这个网站的 DNS 使用的是子域 foo.ourdomain.edu,并从 Windows 2003 域控制器获取它的 DNS。

答案1

编辑 /etc/resolv.conf 并添加 DNS

nameserver 8.8.8.8

nameserver 4.2.2.2

然后:

service networking restart

然后再试一次

第一个是 Google DNS,另一个是来自第 3 级的 DNS

答案2

也许这篇文章会有所帮助:http://curl.haxx.se/mail/lib-2008-07/0067.html

相关内容