我们的 url 是远程 url。我可以通过 IP 地址运行。它抛出错误为“file_get_contents 无法打开流:连接被拒绝”。我已经使用了这个代码。
代码:
$html = file_get_contents('http://xx.xxx.xx.xx:xxxx/apps/index.php');
print_r($html);
var_dump($html);
这个错误是什么?
我们的 url 是远程 url。我可以通过 IP 地址运行。它抛出错误为“file_get_contents 无法打开流:连接被拒绝”。我已经使用了这个代码。
代码:
$html = file_get_contents('http://xx.xxx.xx.xx:xxxx/apps/index.php');
print_r($html);
var_dump($html);
这个错误是什么?
出于安全原因,许多主机会阻止您从远程 URL 加载文件(php.ini 中的 allow_url_fopen 设置)。最好使用 CURL 来下载文件的内容。
<?php
$url = 'http://xx.xxx.xx.xx:xxxx/apps/index.php';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
HTH :)
获取页面需要您打开一个流。像这样的东西:
<?php
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
?>
如需参考,请查看PHP.net:http://php.net/manual/en/function.file-get-contents.php