2015-10-06 39 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>"; 

?>