为什么file_get_contents不起作用?

问题描述 投票:11回答:5

为什么file_get_contents不适合我?在下面的测试文件代码中,似乎我搜索过的所有人的示例都列出了此函数,但它永远不会被执行。这是网络托管服务的问题吗?有人可以在他们的服务器上测试这个代码只是为了看看地理编码数组输出是否实际打印成字符串?当然,我正在尝试将输出分配给变量,但此测试文件中没有输出....

<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>
php geocoding file-get-contents
5个回答
31
投票

检查file_get_contents PHP Manual的返回值。如果值为FALSE则无法读取文件。如果值为NULL,则函数本身被禁用。

要了解file_get_contents操作可能出现的问题,您必须启用错误报告并显示错误以实际读取它们。

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

您可以通过检查服务器上的INI值来获取有关呼叫失败原因的更多详细信息。直接影响file_get_contents函数的一个值是allow_url_fopen。您可以通过运行以下代码来完成此操作。您应该注意,如果它报告不允许fopen,那么您必须要求您的提供商更改服务器上的此设置,以便任何需要此功能的代码才能使用URL。

<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


6
投票

如果PHP的allow_url_fopen ini指令设置为true,并且如果curl也不起作用(请参阅this answer以获取如何使用它而不是file_get_contents的示例),那么问题可能是您的服务器有防火墙阻止脚本获取任意网址的内容(可能允许恶意代码获取内容)。

我遇到了这个问题,并发现我的解决方案是编辑防火墙设置以明确允许对相关域(或IP地址)的请求。


3
投票

如果它是本地文件,则必须将其包装在htmlspecialchars中,如下所示:

    $myfile = htmlspecialchars(file_get_contents($file_name));

然后它工作


3
投票

把你的$adr包裹在urlencode()。我遇到了这个问题,这为我解决了这个问题。


1
投票
//JUST ADD urlencode();
$url = urlencode("http://maps.googleapis.com/maps/api/geocode/json?address=$adr&sensor=false");
<html>
<head>        
<title>Test File</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"> 
</script>
</head>
<body>
<?php    
$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>
© www.soinside.com 2019 - 2024. All rights reserved.