• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

PHP: Parse remote XML Feed....

I am trying to parse this feed, but I seem to be missing something. Instead of getting the categories list, I just get this:
object(SimpleXMLElement)#1 (1) {
  ["@attributes"]=>
  array(2) {
    ["fixed"]=>
    string(3) "yes"
    ["scheme"]=>
    string(52) "http://gdata.youtube.com/schemas/2007/categories.cat"
  }
}

This is the feed:
http://gdata.youtube.com/schemas/2007/categories.cat?hl=en-US

This is the code I have so far:


<?php
	$feedURL = 'http://gdata.youtube.com/schemas/2007/categories.cat?hl=en-US';
    
    // read feed into SimpleXML object
    $xml = simplexml_load_file($feedURL);
    var_dump($xml);
    print_r($xml);
?>

Open in new window

0
interclubs
Asked:
interclubs
  • 4
1 Solution
 
hernst42Commented:
You need to specify the namespace like this:
// read feed into SimpleXML object
$xml = simplexml_load_string($data);
foreach($xml->children("http://www.w3.org/2005/Atom") as $category) {
    echo $category->saveXML(), PHP_EOL;
    /** @var $category SimpleXMLElement */
    foreach ($category->attributes() as $a) {
        /** @var $a SimpleXMLElement */
        echo $a->getName(), (string) $a, PHP_EOL;
    }
}

Open in new window

0
 
InsoftserviceCommented:
not tested as i did not found right xml file

$xmlkey      = file_get_contents($feedURL);
 $xml = simplexml_load_string( $xmlkey );
if(!empty($xml->rootnode)){
foreach( $xml->rootnode as $aClient )
{
echo (string)$aClient->childnote;
}

you can use foreach as per ur need.
searching for good example of xml for testing.but it would work, if proper node name is provided
0
 
InsoftserviceCommented:
hi,
test code is below try it out
<?php
$feedURL = 'http://www.mangalsutrabandhan.com/horoscope.rss';

           $xmlkey      = file_get_contents($feedURL);
             $xml = simplexml_load_string( $xmlkey );

            if(!empty($xml->channel))
            {
                  foreach( $xml->channel as $aClient )
                  {
                        print_r($aClient);
                        echo (string)$aClient->title;
                        echo '<br>';
                        echo (String)$aClient->description;
                        echo '<br>';
                        echo (String)$aClient->link;
                  }
            }
?>
(string),(int) =>is very much important over here. it had created lots of problem for me when i was new for xml part.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
InsoftserviceCommented:
srry missed out uncomment print_r($aClient) .

if you want to check the xml just do echo '<pre>';print_r($xmlkey);print_r($xml); to get the node structure
0
 
interclubsAuthor Commented:
Awesome, thanks!
0
 
InsoftserviceCommented:
did u tried my answer
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now