Parse widget XML code with PHP5

by Pete

We often get asked how to parse our widget XML in PHP. Here's a little parser written in PHP5:

<?php

// Grab this feed URL in your “preview and code” section whenever you create a widget.
$widgetFeedUrl = 'http://affiliate.kickapps.com/kickapps/service/displayBrandableEntityXml.kickAction?b=123';

// load the widget XML feed. 
$s = simplexml_load_file($widgetFeedUrl);

// this will simply dump the name/value pairs, but you can do whatever you want with the data inside these loops

echo '<pre>';
foreach ($s->E as $e) {
  // you’ll typically have one E element per media item (video, audio, photo, blog, user)
  echo 'E: name:' . $e->attributes()->name . '|value:' . $e->attributes()->value . "\n";
  foreach ($e->SE as $se) {
      // the SE elements under the E element will contain the media item (or user)’s properties again as name value pairs
      echo 'SE: name:' . $se->attributes()->name . '|value:' . $se->attributes()->value . "\n";
  }
}
echo '</pre>';

?>

Did you enjoy this tutorial? Have questions, comments, or want to submit one of your own? Contact us at support@kickapps.com.