【问题标题】:both file_get_contents and curl not workingfile_get_contents 和 curl 都不起作用
【发布时间】:2014-01-19 14:56:11
【问题描述】:

我刚刚使用 file_get_contents 来检索我网站中的数据。但它不返回任何东西。

file_get_contents("https://www.google.co.in/images/srpr/logo11w.png")

当我在本地环境中使用它时,它会返回值。

我还在我的网站上尝试了 curl 以确定它是否返回任何内容。 但它显示了

禁止
您没有访问权限。

我搜索了一下,发现了一些提示。我启用了 allow_url_fopenallow_url_include

但没有任何效果。

我尝试过的 curl 代码

function curl($url){ 
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  $return = curl_exec($ch); curl_close ($ch);
  return $return;
} 
$string = curl('https://www.google.co.in/images/srpr/logo11w.png');
echo $string;

【问题讨论】:

  • 你能告诉我们你的 curl 尝试的代码吗? PS:你确定不是你的服务器问题?我刚刚检查过,它在该特定链接上对我来说很好。
  • @Dainis Ablos:我已经更新了这个问题。看看就好。
  • 正如我所说,您的主机可能会阻止对外部资源的调用。 Here 是您在我的服务器上未更改的脚本,它工作得很好。
  • vinothavn - 你查到真相了吗?我有同样的问题。我已经确认 allow_url_fopen 设置为“开”。如果有人对“主机可能阻止对外部资源的调用”(根据@Dainis Abols 的 cmets)有任何想法,我也会很感激 - 在这种情况下,外部资源实际上最终是本地的,但它是渲染的( json) 本地文件的输出,而不是我想要的文件源的内容。

标签: php curl file-get-contents phpinfo


【解决方案1】:

file_get_contents() 方式...

<?php
header('Content-Type: image/png');
echo file_get_contents("https://www.google.co.in/images/srpr/logo11w.png");

cURL 方式...

<?php
header('Content-Type: image/png');
function curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $return = curl_exec($ch); curl_close ($ch);
    return $return;
}
$string = curl('https://www.google.co.in/images/srpr/logo11w.png');
echo $string;

输出:

【讨论】:

  • 感谢您的帮助。在本地环境中显示图像,但在我的网站中显示 图像无法显示,因为它包含错误
  • 我检查了我的服务器,即使使用https 也能通过,所以我认为 OPs 主机不允许调用外部资源。
  • @DainisAbols,也许你是对的。 vinoth,cURL 呢?
猜你喜欢
  • 1970-01-01
  • 2016-09-13
  • 2014-01-19
  • 2014-10-13
  • 2014-06-23
  • 1970-01-01
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多