2015-10-06 57 views
-3

什麼是解析XML飼料,看起來像這樣的方式:PHP解析XML與使用simplexml_load_file

<string xmlns="http://vs-social-feed/"> 
{ "response": "success", "message": "", "feed": 
[ 
{"SocialId":105,"Type":2,"Id":"202297433163449_956836567709528","Url":"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956836567709528/?type=3","Text":"Hold your tongue!\n\nStacy Martin by Ellen von Unwerth official for Vs. Magazine","Other":"\u003ca href=\"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956836567709528/?type=3\" target=\"_blank\"\u003e\u003cimg src=\"https://graph.facebook.com/956836567709528/picture/\" \" width=\"220\"\u003e\u003c/a\u003e","DateCreate":"\/Date(1444068001000)\/"}, 
{"SocialId":104,"Type":2,"Id":"202297433163449_956833817709803","Url":"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956833817709803/?type=3","Text":"Proper posture, ladies!\n\nThe always elegant, Christy Turlington Burns by Patrick Demarchelier","Other":"\u003ca href=\"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956833817709803/?type=3\" target=\"_blank\"\u003e\u003cimg src=\"https://graph.facebook.com/956833817709803/picture/\" \" width=\"220\"\u003e\u003c/a\u003e","DateCreate":"\/Date(1444075201000)\/"} 
] 
} 
</string> 

我試圖與使用simplexml_load_file,的file_get_contents,json_decode和JSON編碼,但沒有任何運氣。

+0

思想是用XML,因爲飼料被包裹在<字符串的xmlns = 「HTTP://社會飼料/」> ... – andkjaer

+0

你將不得不從JSON值先將XML解碼。 – ThW

回答

0

它是JSON數據不是XML。 PHP內置了json_decode()函數來解析json數據。

試試這個代碼:

<?php 

$data = '{ "response": "success", "message": "", "feed":[{"SocialId":105,"Type":2,"Id":"202297433163449_956836567709528","Url":"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956836567709528/?type=3","Text":"Hold your tongue!\n\nStacy Martin by Ellen von Unwerth official for Vs. Magazine","Other":"\u003ca href=\"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956836567709528/?type=3\" target=\"_blank\"\u003e\u003cimg src=\"https://graph.facebook.com/956836567709528/picture/\" \" width=\"220\"\u003e\u003c/a\u003e","DateCreate":"/Date(1444068001000)/"},{"SocialId":104,"Type":2,"Id":"202297433163449_956833817709803","Url":"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956833817709803/?type=3","Text":"Proper posture, ladies!\n\nThe always elegant, Christy Turlington Burns by Patrick Demarchelier","Other":"\u003ca href=\"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956833817709803/?type=3\" target=\"_blank\"\u003e\u003cimg src=\"https://graph.facebook.com/956833817709803/picture/\" \" width=\"220\"\u003e\u003c/a\u003e","DateCreate":"/Date(1444075201000)/"}] }'; 
$obj = json_decode($data); 
echo "<pre>"; print_r($obj->feed); echo "</pre>"; 

?>