[英]Converting External XML feed to JSON
我正在嘗試將xml提要轉換為json,但是使用此代碼將無法執行任何操作。 有任何想法嗎?
<?php
$xml_string = 'https://xml.betfred.com/Horse-Racing-Daily.xml';
//read the XML file
$xml = file_get_contents($xml_string);
//encode the formatted data
$json = json_encode();
//generate the JSON file
header('Content-Type: application/json');
echo $json;
?>
您忘記將$xml
放在json_encode()
方法中作為參數。 像這樣調整代碼:
$json = json_encode($xml);
但是,這只會將$xml
字符串轉換為json,而這可能不是您想要的。
要接近您要嘗試做的事情,您可以執行以下操作:
$simpleXml = simplexml_load_string($xml);
$json = json_encode($simpleXml);
您的json可能包含很多垃圾,但也許足以滿足您的需求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.