php file_get_contents 适用于本地但不适用于服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10982595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:31:49  来源:igfitidea点击:

file_get_contents works on Local but not on server

phpfile-get-contents

提问by blytung

Possible Duplicate:
file_get_contents() error

可能重复:
file_get_contents() 错误

Working on a script that connects to Instagram API to get photos from a certain tag. It works just fine on Local but on the server i get errors.

处理连接到 Instagram API 以从特定标签获取照片的脚本。它在本地工作得很好,但在服务器上我得到了错误。

Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /storage/content/91/103391/instaboll.nu/public_html/instagram.php on line 19.

警告:file_get_contents(): https:// 包装器在服务器配置中被 /storage/content/91/103391/instaboll.nu/public_html/instagram.php 中的 allow_url_fopen=0 在第 19 行禁用。

This is how line 19 looks:
$contents = file_get_contents("https://api.instagram.com/v1/tags/$tag/media/recent?client_id=$client_id");

这是第 19 行的样子:
$contents = file_get_contents(" https://api.instagram.com/v1/tags/$tag/media/recent?client_id=$client_id");

Any ideas?

有任何想法吗?

Have searched for this and find some posts that recommend curl, but have no idea how to proceed with that.

已经搜索过这个并找到了一些推荐 curl 的帖子,但不知道如何进行。

回答by blytung

I solved it with this code.

我用这段代码解决了它。

<?php
$url = "http://www.example.org/";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
if (curl_errno($ch)) {
  echo curl_error($ch);
  echo "\n<br />";
  $contents = '';
} else {
  curl_close($ch);
}

if (!is_string($contents) || !strlen($contents)) {
echo "Failed to get contents.";
$contents = '';
}

echo $contents;
?>    

回答by Explosion Pills

The solution is in the error message: set allow_url_fopento 1in your ini (use a php.inifile, use ini_setin php code, or talk to your host).

解决方案在错误消息中:在您的 ini 中设置allow_url_fopen1(使用php.ini文件、ini_set在 php 代码中使用或与您的主机交谈)。

回答by Sarfraz

You will have to enable allow_url_fopenfrom php.ini on your host or talk to your hosting providers.

您必须allow_url_fopen从主机上的 php.ini启用或与您的主机提供商交谈。

This option enables the URL-aware fopen wrappers that enable accessing URL object like files.

此选项启用支持访问 URL 对象(如文件)的 URL 感知 fopen 包装器。

回答by cdhowie

Your server admin has disabled this functionality. You'll need to ask them to enable it.

您的服务器管理员已禁用此功能。您需要让他们启用它。