PHP 包含网页返回空白或错误​​的所有方法

All methods of including webpage returning blank or error in PHP

我试图包含一个网页的内容,它在本地主机上工作正常,但是当我将文件上传到服务器时它不再工作

我试过 file_get_contents()cURLfopen,它们在我的电脑上都可以正常工作,但在线时失败。

我的主机在 PHP.ini 中是否设置了一个变量来阻止我获取其他页面的内容?

<?php


$url = 'http://www.example.com';

echo stream_get_contents( fopen($url, 'r') );

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);     

echo $output;


echo file_get_contents( $url );
?> 

错误:

Warning: fopen(http://www.example.com): failed to open stream: Network is unreachable in /srv/disk12/1999559/www/techmex.co.nf/test.php on line 6

Warning: stream_get_contents() expects parameter 1 to be resource, boolean given in /srv/disk12/1999559/www/techmex.co.nf/test.php on line 6

Warning: file_get_contents(http://www.example.com): failed to open stream: Network is unreachable in /srv/disk12/1999559/www/techmex.co.nf/test.php on line 17

大多数网络服务器默认禁用此功能,但如果您有权更改 php.ini,则应设置 "allow_url_fopen = On"。

您也可以试试:

<?php
    ini_set('allow_url_fopen', 1);
    echo file_get_contents('http://www.example.com');
?>