2013-10-15 81 views
0

我有一個錯誤使用捲曲爲PHP XML生成的HTTP響應

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity 

我有同樣的服務器上的兩個PHP文件和我想接收從PHP生成XML

update.php http響應

<?php 

$url = '[pathtofile]response.php'; 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

/* Get the HTML or whatever is linked in $url. */ 
$response = curl_exec($ch); 

$xml = simplexml_load_file($response); 

curl_close($ch); 

?> 

和response.php

<?php 

header("Content-type: text/xml"); 
echo "<?xml version='1.0' encoding='ISO-8859-1'?>"; 
echo "<note>"; 
echo "<from>Jani</from>"; 
echo "<to>Tove</to>"; 
echo "<message>Remember me this weekend</message>"; 
echo "</note>"; 
?> 

什麼原因導致錯誤?

+1

'simplexml_load_file'需要包含文件名的字符串,你想'simplexml_load_string'。請參閱手冊http://php.net/manual/en/function.simplexml-load-string.php –

+0

Pekka是正確的 - 您已經有字符串,您使用了錯誤的函數來解釋它(您嘗試訪問一個文件,其名稱是您提取的文件的內容...) - 並且由於這樣的文件不存在,您會看到您看到的錯誤。 – Floris

+0

謝謝你的工作 – aav

回答

0

用這個例子:

<?php 
function download_page($path){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$path); 
    curl_setopt($ch, CURLOPT_FAILONERROR,1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 15); 
    $retValue = curl_exec($ch);   
    curl_close($ch); 
    return $retValue; 
} 

$sXML = download_page('http://alanstorm.com/atom'); 
$oXML = new SimpleXMLElement($sXML); 

foreach($oXML->entry as $oEntry){ 
    echo $oEntry->title . "\n"; 
}