为什么file_get_contents不起作用? [英] Why doesn't file_get_contents work?

查看:98
本文介绍了为什么file_get_contents不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么file_get_contents 对我有用?在下面的测试文件代码中,似乎我搜索过的每个人的示例都列出了此函数,但从未执行过.这是网络托管服务的问题吗?有人可以在他们的服务器上测试此代码,只是看地理编码数组的输出是否真的以字符串形式打印出来了吗?当然,我正在尝试将输出分配给变量,但是此测试文件中没有输出....

Why does file_get_contents not work for me? In the test file code below, it seems that everyone's examples that I've searched for all have this function listed, but it never gets executed. Is this a problem with the web hosting service? Can someone test this code on their server just to see if the geocoding array output actually gets printed out as a string? Of course, I am trying to assign the output to a variable, but there is no output here in this test file....

<html>
<head>        
<title>Test File</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"> 
</script>
</head>
<body>
<?    
$adr = 'Sydney+NSW';
echo $adr;
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$adr&sensor=false";
echo '<p>'.$url.'</p>';
echo file_get_contents($url);
print '<p>'.file_get_contents($url).'</p>';
$jsonData   = file_get_contents($url);
echo $jsonData;
?>
</body>
</html>

推荐答案

检查 file_get_contents PHP手册 返回值.如果值为FALSE,则无法读取文件.如果值为NULL,则该功能本身将被禁用.

Check file_get_contents PHP Manual return value. If the value is FALSE then it could not read the file. If the value is NULL then the function itself is disabled.

要了解更多有关file_get_contents操作可能出什么问题的信息,必须启用错误报告功能并显示错误以实际读取它们.

To learn more what might gone wrong with the file_get_contents operation you must enable error reporting and the display of errors to actually read them.

# Enable Error Reporting and Display:
error_reporting(~0);
ini_set('display_errors', 1);

通过检查服务器上的INI值,可以获得有关呼叫失败原因的更多详细信息.直接影响file_get_contents函数的一个值是 allow_url_fopen .您可以通过运行以下代码来做到这一点.您应该注意,如果它报告不允许使用fopen,则必须要求提供程序在服务器上更改此设置,以便所有需要此功能的代码都可以与URL一起使用.

You can get more details about the why the call is failing by checking the INI values on your server. One value the directly effects the file_get_contents function is allow_url_fopen. You can do this by running the following code. You should note, that if it reports that fopen is not allowed, then you'll have to ask your provider to change this setting on your server in order for any code that require this function to work with URLs.

<html>
    <head>        
        <title>Test File</title>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
        </script>
    </head>
    <body>
<?php

# Enable Error Reporting and Display:
error_reporting(~0);
ini_set('display_errors', 1);

$adr = 'Sydney+NSW';
echo $adr;
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$adr&sensor=false";
echo '<p>'.$url.'</p>';

$jsonData = file_get_contents($url);

print '<p>', var_dump($jsonData), '</p>';

# Output information about allow_url_fopen:
if (ini_get('allow_url_fopen') == 1) {
    echo '<p style="color: #0A0;">fopen is allowed on this host.</p>';
} else {
    echo '<p style="color: #A00;">fopen is not allowed on this host.</p>';
}


# Decide what to do based on return value:
if ($jsonData === FALSE) {
    echo "Failed to open the URL ", htmlspecialchars($url);
} elseif ($jsonData === NULL) {
    echo "Function is disabled.";
} else {
   echo $jsonData;
}

?>
    </body>
</html>

如果所有操作均失败,则可能是由于使用了短打开标签<?.因此,此答案中的示例代码已更改为使用<?php可以正常工作,因为无论设置了什么配置选项,都保证可以在所有版本的PHP上使用它.要针对自己的脚本执行此操作,只需替换<?<?php.

If all of this fails, it might be due to the use of short open tags, <?. The example code in this answer has been therefore changed to make use of <?php to work correctly as this is guaranteed to work on in all version of PHP, no matter what configuration options are set. To do so for your own script, just replace <? or <?php.

这篇关于为什么file_get_contents不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆