How to Parse XML Sports Feeds

Parsing, or reading, XML feeds that contain sports news can be done by using the programming language PHP. There are a few simple PHP functions that can be used to grab data from XML feed files and pass them on to a front-end display that allows users browsing your website to read the sports news coming from the feed.

Instructions

    • 1

      Open a text editor where you can begin to program a new PHP-scripted application.

    • 2

      Obtain the XML feed by using the following code and replacing the URL in the example with the URL of the sports-news XML feed that you wish to use:

      $xml = file_get_contents('http://www.examplefeed.com/sportsfeed.xml');

    • 3

      Allow PHP to open the XML URLs by using the "cURL" PHP function as follows:

      $ch = curl_init()l

      curl_setopt($ch, CURLOPT_URL,

      'http://www.examplefeed.com/sportsfeed.xml');

      curl_setopt($ch, CURLOPT_HEADER, false);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

      $xml = curl_exec($ch);

      curl_close($ch);

    • 4

      Create an array to host the information passed from the XML feed by using the following code, which assumes that one of the data parameters being passed from XML is named "sports_news":

      $sports_news = element_set('item, $xml);

    • 5

      Dictate what is placed in each array by using the following example of code and modifying it to fit the parameters passed from your XML feed:

      foreach($sports_news as $item) {

      $title = value_in('title', $item);

      $URL = value_in('link', $item);

      $news_article = value_in ('news_article' $item);

      $item_array[] = array(

      'title' => $title,

      'URL' => $URL,

      'news_article' => $news_article

      );

      }

Related Searches:

References

Comments

Related Ads

Featured