Check file_get_contents PHP Manual http://php.net/file_get_contents 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
操作时,您必须启用错误报告和错误显示才能实际读取它们。
# Enable Error Reporting and Display:
error_reporting(~0);
ini_set('display_errors', 1);
您可以通过检查服务器上的 INI 值来获取有关调用失败原因的更多详细信息。一个值直接影响file_get_contents
函数是allow_url_fopen http://www.php.net/manual/en/filesystem.configuration.php#ini.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);
echo '<pre>', htmlspecialchars(substr($jsonData, 128)), sprintf(' ... (%d)', strlen((string)$jsonData)), '</pre>';
# 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 '<pre>', htmlspecialchars($jsonData), '</pre>';
}
?>
</body>
</html>
如果所有这些都失败,可能是由于使用了短开放标签,<?
。因此,该答案中的示例代码已更改为使用<?php
正确工作,因为无论设置什么配置选项,这都保证在所有版本的 PHP 中工作。要为您自己的脚本执行此操作,只需替换<?
or <?php
.