cURL 没有得到 URL 的 HTML 来源

cURL not getting HTML source of URL

我正在尝试使用 PHP 制作一个简单的网络爬虫,但在获取给定 URL 的 HTML 源时遇到问题。我目前正在使用 cURL 来获取源代码。

我的代码:

 $url = "http://www.nytimes.com/";

    function url_get_contents($Url) {
        if (!function_exists('curl_init')) {
            die('CURL is not installed!');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $Url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);
        if ($output === false) { die(curl_error($ch)); }
        curl_close($ch);
        return $output;
    }

    echo url_get_contents($url);
    ?>

现在没有任何回应,也没有任何错误,所以有点神秘。任何建议或修复将不胜感激

编辑:我添加了

if ($output === false) { die(curl_error($ch)); }

到函数的中间,它最终给我一个错误(终于!):

无法解析主机:www.nytimes.com

我还是不太清楚问题出在哪里。有什么想法吗?

谢谢

可变大小写不匹配($url$Url)。变化:

function url_get_contents($Url) {

function url_get_contents($url) {

事实证明这不是 cURL 问题

我的主机服务器(Ubuntu 虚拟机)正在使用 "host-only" 网络适配器工作,该适配器阻止访问其主机以外的所有其他 IP 或域,导致 cURL 无法连接到网址。

一旦更改为 "bridged" 网络适配器,我就可以访问外部世界了。

希望对您有所帮助。