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

//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;








More info here;