Tag Archives: local

Cakephp 3- How to read a remote Xml file

23666One of the great things about CakePHP is that it has a lot of built in librarys.  Heres an example of how to read a remote XML file.

 

 

 

use Cake\Utility\Xml;
use Cake\Network\Http\Client;

//XML file you want to read in
$url="http://store.steampowered.com/feeds/newreleases.xml";

//read a remote xml file
$http = new Client();
$response = $http->get($url);
$xml = Xml::build($response->body());


//Method 1 using SimpleXMLElement Object
//loop the item elements array of xml file
foreach($xml->channel->item as $item){
	//need to cast to string
	debug ((string)$item->title);
	debug ((string)$item->link);
}


//Method 2 convert xml to array
$xml = Xml::toArray($xml);

//loop elements of array
foreach($xml['rss']['channel']['item'] as $item){
	debug ('Title:'.$item['title']);
	debug ('Link:'.$item['link']);
}

The result will print out the list of items from the feed;
http://store.steampowered.com/feeds/newreleases.xml

cakexml

 

 

 

 

 

 

More info here;
http://book.cakephp.org/3.0/en/core-libraries/xml.html#importing-data-to-xml-class