Jump to content

Trouble parsing an rss/xml feed to array


Xdega

Recommended Posts

hi.

Basically, I am trying to set up a random bible verse script. I found an rss/xml feed here:

http://www.mybiblescripture.com/rss/bible.php?fmt=daily&trans=KJV

 

Using the following code, I am attempting to parse the feed in to an array:

 

<?php
function fetchXML($url) 
{ 
//initialize library 
$ch = curl_init(); 
//used to make us look like a browser 
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"; 
//set the url 
curl_setopt ($ch, CURLOPT_URL, $url); 
//set that I want to wait for a response 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
//make us look like a web browser 
curl_setopt ($ch, CURLOPT_USERAGENT, $useragent); 
//get data 
$data = curl_exec($ch); 
//clean up 
curl_close($ch); 
//return xml data 
return $data; 
} 
define("USE_CURL", true); 
$url = "http://www.mybiblescripture.com/rss/bible.php?fmt=daily&trans=KJV";
//get xml doc with info 
$data = fetchXML($url); 
//create a SimpleXML object to parse the xml 
$char_xml = new SimpleXmlElement($data); 
echo print_r($char_xml);
?>

 

Which gives me the following output:

SimpleXMLElement Object ( [@attributes] => Array ( [version] => 2.0 ) [channel] => SimpleXMLElement Object ( [title] => My Bible Scripture - Bible Verse of the Day (KJV) [link] =>
http://www.mybiblescripture.com/ [description] => Verse of the Day - King James Version [language] => en-us [pubDate] => SimpleXMLElement Object ( ) [generator] => CPG-Nuke
Dragonfly [copyright] => My Bible Scripture [category] => Religion [image] => SimpleXMLElement Object ( [width] => 111 [height] => 40 [url] =>
http://www.mybiblescripture.com/images/logo.gif [title] => My Bible Scripture [link] => http://www.mybiblescripture.com/ ) [item] => Array ( [0] => SimpleXMLElement Object ( [title] =>
Corinthians I 10:17 [link] => http://www.mybiblescripture.com/Bible/l_op=view-verse/lid=29613/trans=KJV.html [description] => For we being many are one bread, and one body: for we are
all partakers of that one bread. (KJV) [pubDate] => SimpleXMLElement Object ( ) ) [1] => SimpleXMLElement Object ( [title] => Bible Verses [link] => http://www.mybiblescripture.com
[description] => Find more Bible Scripture [pubDate] => Fri, 11 Mar 2011 00:45:06 GMT ) ) ) ) 1

 

 

What I am trying to do is pull ONLY [title] => Corinthians I 10:17 and its accompanying verse [description] => For we being many are one bread, and one body: for we are all partakers of that one bread. (KJV).

 

I am very bad at trying to read multi-dim arrays and can't for the life of me figure out how to print the aforementioned keys.

 

Could anyone help me here?

thx

Link to comment
Share on other sites

additionally, if anyone knows of any software or apps that can be used to read multitim arrays in a more human readable form would be great.

Eg something that i could plug an array in to, and then be given a list of all the available keys and the paths used to print them etc.

Link to comment
Share on other sites

cool. here:

 

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 2.0
        )

    [channel] => SimpleXMLElement Object
        (
            [title] => My Bible Scripture - Bible Verse of the Day (KJV)
            [link] => http://www.mybiblescripture.com/
            [description] => Verse of the Day - King James Version
            [language] => en-us
            [pubDate] => SimpleXMLElement Object
                (
                )

            [generator] => CPG-Nuke Dragonfly
            [copyright] => My Bible Scripture
            [category] => Religion
            [image] => SimpleXMLElement Object
                (
                    [width] => 111
                    [height] => 40
                    [url] => http://www.mybiblescripture.com/images/logo.gif
                    [title] => My Bible Scripture
                    [link] => http://www.mybiblescripture.com/
                )

            [item] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [title] => Corinthians I 10:17
                            [link] => http://www.mybiblescripture.com/Bible/l_op=view-verse/lid=29613/trans=KJV.html
                            [description] => For we being many are one bread, and one body: for we are all partakers of that one bread. (KJV)
                            [pubDate] => SimpleXMLElement Object
                                (
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [title] => Bible Verses
                            [link] => http://www.mybiblescripture.com
                            [description] => Find more Bible Scripture
                            [pubDate] => Fri, 11 Mar 2011 04:15:31 GMT
                        )

                )

        )

)

 

definitely makes the structure clearer and easier to read.

Link to comment
Share on other sites

well. Thanks to the advice on the <pre> for printing the array, I was able to nail it with the following code:

 

<?php
$scripture_a=$char_xml->channel->item->description;
$scripture_b=$char_xml->channel->item->title;

echo $scripture_a . '<br><b>'. $scripture_b. '</b>';
?>

 

Thank you so much, now knowing how to correctly print an array like that, will help me HUGELY in the future.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.