我想使用 php 从远程服务器获取 xml 数据。这是我获取 xml 的代码,但我得到了 bool(false)
<?php
header ("content-type: text/plain");
$filename = file_get_contents("http://gep4.com/radio/wp-content/plugins/haze_radio/readme.txt");
var_dump($filename);
?>
试试这个来获取它正在工作的数据
function getPage($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com/');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8');
$result['EXE'] = curl_exec($ch);
$result['INF'] = curl_getinfo($ch);
$result['ERR'] = curl_error($ch);
curl_close($ch);
unset($url, $referer, $agent, $header, $timeout);
return $result;
}
$url = "http://gep4.com/radio/wp-content/plugins/haze_radio/readme.txt";
var_dump(getPage($url) );
这意味着获取文件内容时出错。您是否确定该文件存在或您有权读取该文件或其他内容?
返回值
该函数返回读取的数据或失败时返回 FALSE
可能allow_url_fopen在 php.ini 中被禁用
如果您有权访问 php.ini,请启用它
或者,尝试CURL
示例 CURL 调用
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);