从php中的远程服务器获取xml数据

时间:2012-11-05 11:55:19

标签: php jquery xml

我想使用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);
?>

3 个答案:

答案 0 :(得分:0)

这意味着获取文件内容时出错。您是否确定该文件存在或您是否有权读取该文件或其他内容?

  

file_get_contents

     

返回值

     

该函数返回读取数据或失败时返回FALSE

答案 1 :(得分:0)

可能在php.ini中禁用了allow_url_fopen

如果您有权访问ot 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); 

答案 2 :(得分:0)

尝试此操作以获取正在运行的数据

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) );