Ikari
Ikari

Reputation: 3236

There is a error in parsing the rss from stackoverflow.com. using SimpleXML in PHP

I was trying to parse the rss of the tag PHP, from http://stackoverflow.com and tried to use something other than DOM Model, So I looked into SimpleXML. THis is my code:

<?php
    error_reporting(-1);
    $xml = file_get_contents('https://stackoverflow.com/feeds/tag/php');
    $loaded = simplexml_load_string($xml) or die("There is a problem");
    $str1 = $loaded["entry"]["entry"][0]->title;
    echo $str1;
?>

But nothing is displayed on the screen, and also no error is displayed! The sample data from https://stackoverflow.com/feeds/tag/php can be found at http://gourabt2.cloudapp.net/sample-data/sample_data.xml

Any Help would be very much appreciated! Thanks! Cheers!

Upvotes: 0

Views: 150

Answers (3)

hakre
hakre

Reputation: 197554

You use array-access in SimpleXML to access attributes so:

$loaded["entry"]

returns the attribute named "entry" from the document element.

use arrow-access to get the element named "entry" instead:

$loaded->entry

this returns the element named "entry".

Additionally take care with namespaces. Parsing a feed with SimpleXML has been outlined already in existing Q&A material, please relate to it.

Upvotes: 1

Jonathan
Jonathan

Reputation: 2877

That's because you have the wrong path. Here is the correction.

(string)$loaded->entry->title;

Upvotes: 0

chris85
chris85

Reputation: 23892

Try this out.

<?php
    $xml = file_get_contents('http://stackoverflow.com/feeds/tag/php');
    $loaded = simplexml_load_string($xml) or die("There is a problem");
    foreach($loaded->entry as $post) {
       echo $post->title . "\n";
    }

Output:

join 2 tables - group by id order by date price asc
using php variable in an sql query
Clear browser cache memory by php code
There is a error in parsing the rss from stackoverflow.com. using SimpleXML in PHP
Modify Laravel Validation message response
chained dropdown from database
Php database handling
How to load model with Codeigniter Webservice - Nusoap Server?
multiple report download php adwords api
Unable to confirm Amazon SNS subscription for SES bounces
Comparing if values exist in database
Better way to CURL to WCF
PHP SaaS Facebook app: Host Page Tab ID and User ID
PHP and Mysql - running over PDOStatement
How to change form textbox value in Zend form annotation?
connect Android with PHP, MySQL , unfortunately app has stopped in android emulator
Auto increment a SESSION key ID
Call PHP function in a class from a HTML form
PHP SQL Preventing Duplicate User names: Catching Exception vs Select Query
I am only able to grab the first group of text in between the tr need helping fixing my code
How to run an external program in php
How to connect to php in android using an async task?
PHP Return HTML (Laravel / Lumen Framework)
prestashop smarty if statement
Cakephp OR condition Implementation
Progress bar HTML5/PHP
preg_match file url from jwplayer
Does Cloudflare Cache HTML5 Video Embedded on PHP Page?
how to INSERT INTO JOIN
PHP web service returns "403 forbidden" error

Upvotes: 1

Related Questions