我想使用MediaWiki的API獲取XML格式的文章,並將它們包括在我的頁面上。我創建了一個簡單的代碼,它基本上使用?action=parse&page=Page_Name&format=xml
請求獲取文章的XML表示形式。代碼如下:PHP的html_entity_decode和HTML <a>標籤
if($_GET["page"]=='') die("Page not specified (possibly direct call)");
$pagename = $_GET["page"];
$handle = @fopen("mediawiki/api.php?action=parse&page=".$pagename."&format=xml", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = $buffer.fgets($handle);
}
$buffer = html_entity_decode($buffer);
/*
echo $buffer;
*/
$xml = simplexml_load_string($buffer);
foreach($xml->parse->children() as $child){
switch($child->getName()){
case "text":
echo $child->asXML()."<br/>";
break;
case "categories":
echo "<h3>Categories this project is related to: </h3><br/>";
foreach($child->children() as $grandChild){
echo $grandChild." | ";
}
break;
}
}
fclose($handle);
}
現在的問題是,我越來越奇怪的輸出。任何<a name="" href=""></a>
變成<a name="" href=""/>
,這使得所有以下文本成爲一個鏈接(我猜想,因爲沒有結束標籤</a>
)。在Mozilla Firefox和Google Chrome中都可以看到這一點。我懷疑$buffer = html_entity_decode($buffer);
導致此問題。是否有一個html_entity_decode();
的參數,我應該指定以避免這種情況?是否由我的代碼中的其他錯誤或html_entity_decode();
錯誤引起?
(要查看維基的API的XML輸出,你可以嘗試http://en.wikipedia.org/w/api.php?action=parse&page=No_Such_Page&format=xml
不同page
參數)
可能的解決方案:我不想去JSON,因爲喬丹的建議,所以我來了此解決方案。我只是將html_entity_decode
移至case "text":
區塊。所以現在我在那裏echo html_entity_decode($child->asXML())."<br/>";
。你認爲這足夠可行嗎?
@Azimuth,你去了!將其粘貼到textarea中,選擇它並按下Ctrl-K縮進它所有4個空格(或者在該代碼的情況下,相關的東西已經縮進4個空格,所以我只是複製並粘貼它) – 2009-12-11 16:55:44
''是一個空的元素,在XML中可以自行關閉到''. – 2009-12-11 16:56:47
@Dominic is that the browsers' problem then? Because as I wrote both FF and Chrome output it so that all text becomes a link... Thanks for putting the code – Azimuth 2009-12-11 16:58:25