{"id":552,"date":"2008-11-17T19:55:38","date_gmt":"2008-11-17T19:55:38","guid":{"rendered":"http:\/\/west-penwith.org.uk\/blog\/?p=552"},"modified":"2014-04-11T18:27:57","modified_gmt":"2014-04-11T17:27:57","slug":"parsing-xml-with-php","status":"publish","type":"post","link":"https:\/\/west-penwith.org.uk\/blog\/archives\/552","title":{"rendered":"Parsing XML with PHP"},"content":{"rendered":"<p>Following on from the <a href=\"http:\/\/west-penwith.org.uk\/blog\/archives\/532\">last.fm saga described earlier<\/a>, I went on to look at the method <code>user.GetWeeklyAlbumChart<\/code> which requires accessing <code>user.GetWeeklyChartList<\/code> first.<\/p>\n<p>I am using the plugin <a href=\"http:\/\/leandrow.net\/lastfm\/\">iLast.Fm<\/a> from Leandro Alonso and the code he is using looks right but doesn&#8217;t seem to work. He uses <code>curl<\/code> to get the XML from the <code>last.fm<\/code> site. The XML you get is of the form<\/p>\n<blockquote>\n<p>\n<code>&lt;lfm status=\"ok\"&gt;<br \/>\n  &lt;weeklychartlist user=\"[username]\"&gt;<br \/>\n    &lt;chart from=\"1225022400\" to=\"1225627200\"\/&gt;<br \/>\n    &lt;chart from=\"1225627200\" to=\"1226232000\"\/&gt;<br \/>\n    &lt;chart from=\"1226232000\" to=\"1226836800\"\/&gt;<br \/>\n  &lt;\/weeklychartlist&gt;<br \/>\n&lt;\/lfm&gt;<\/code>\n<\/p>\n<\/blockquote>\n<p>He then parses it with <code><a href=\"http:\/\/uk.php.net\/simplexml_load_file\">simplexml_load_file()<\/a><\/code> and puts it into an object called <code>$chart<\/code>. Then the code processes this as follows<\/p>\n<blockquote>\n<p>\n<code>$chartopt = sizeof($chart->weeklychartlist->chart) - 1;<br \/>\n$chart = $chart->weeklychartlist->chart[$chartopt];<\/code>\n<\/p>\n<\/blockquote>\n<p>and uses <code>$chart['from']<\/code> and <code>$chart['to']<\/code> in the call to <code>user.GetWeeklyAlbumChart<\/code>.<\/p>\n<p>The problem is that <code>$chartopt<\/code> always has the value 0 which means that the <code>sizeof()<\/code> function is not working properly. There is a comment on the <a href=\"http:\/\/uk.php.net\/manual\/en\/function.simplexml-load-file.php#86471\">PHP documenattion page<\/a> which says that <code>foreach<\/code> doesn&#8217;t work but reccomends <code>count\/sizeof()<\/code> instead. What can be wrong?<\/p>\n<p>Update: The answer seems to be <a href=\"http:\/\/vega.rd.no\/article\/simplexml-not-that-simple\">here<\/a>: SimpleXML is not so simple and it doesn&#8217;t behave correctly. It needs<\/p>\n<blockquote>\n<p>\n<code>$chartopt = -1;<br \/>\nforeach($chart->weeklychartlist->chart as $i) $chartopt++;<br \/>\n<\/code>\n<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Following on from the last.fm saga described earlier, I went on to look at the method user.GetWeeklyAlbumChart which requires accessing user.GetWeeklyChartList first. I am using the plugin iLast.Fm from Leandro Alonso and the code he is using looks right but doesn&#8217;t seem to work. He uses curl to get the XML from the last.fm site. [&hellip;]<\/p>\n","protected":false},"author":239,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,5,10],"tags":[],"class_list":["post-552","post","type-post","status-publish","format-standard","hentry","category-hi-fi","category-technical","category-wordpress"],"_links":{"self":[{"href":"https:\/\/west-penwith.org.uk\/blog\/wp-json\/wp\/v2\/posts\/552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/west-penwith.org.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/west-penwith.org.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/west-penwith.org.uk\/blog\/wp-json\/wp\/v2\/users\/239"}],"replies":[{"embeddable":true,"href":"https:\/\/west-penwith.org.uk\/blog\/wp-json\/wp\/v2\/comments?post=552"}],"version-history":[{"count":3,"href":"https:\/\/west-penwith.org.uk\/blog\/wp-json\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":1755,"href":"https:\/\/west-penwith.org.uk\/blog\/wp-json\/wp\/v2\/posts\/552\/revisions\/1755"}],"wp:attachment":[{"href":"https:\/\/west-penwith.org.uk\/blog\/wp-json\/wp\/v2\/media?parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/west-penwith.org.uk\/blog\/wp-json\/wp\/v2\/categories?post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/west-penwith.org.uk\/blog\/wp-json\/wp\/v2\/tags?post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}