卷曲不';t获取外部网站


Curl doesn't get external websites

我在本地计算机(我自己的计算机)上的cUrl配置有一些问题。

我只能下载放在本地主机上的网站。尝试任何其他主机都会导致失败(返回空字符串)。我确信代码是可以的——它在我的生产服务器上运行。

此外,curl_errno不会返回任何错误。

我找不到问题,请帮忙。

编辑:这是代码。

<?php
$url = "http://stackoverflow.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); // It's empty

您的机器上是否安装了PHP curl,如果没有,请先安装它,然后尝试

对于安装,您可以使用以下任一方法

  • http://php.net/manual/en/curl.installation.php
  • sudoapt-get安装php5-curl,然后重新启动apache

它应该工作

你可能想试试这个而不是卷曲

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$url = "http://stackoverflow.com";
echo file_get_contents($url);

还要确保在php.ini中启用了allow_url_fopenhttp://www.php.net/manual/en/filesystem.configuration.php#ini.allow-打开的url

回声结果[已测试并工作]

<?php
$url = "http://stackoverflow.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); 
echo $result;//Added