Help with Parsing XML with PHP

I want to extract the <text></text> parts of the xml output of
an api into an array then get the actual text out, I wrote something
like this :

$apiurl=urlencode("http://api.preachingcentral.com/bible.php?passage=$passage&version=$version");
$data = file_get_contents('$apiurl');
$bible = new SimpleXMLElement($data,0,true);
$dtext=array();
foreach ($bible as $item)
{
  $dtext[]=$item->text;
 }

It didn’t work, no message whatsoever displayed.

Can someone please help to rectify the code.

The api url is as follows, you can test with $passage=Genesis1:1-2&version=kjv

http://api.preachingcentral.com/bible.php?passage=$passage&version=$version

Thanks.

json_encode/decode are great functions to transform XML into associative arrays.


<?php
$passage='Genesis1:1-2';
$version='kjv';
$url = "http://api.preachingcentral.com/bible.php?passage=$passage&version=$version";
$xml = new SimpleXMLElement(file_get_contents($url));
$data = json_decode(json_encode($xml), true);

// Print the text portions you want to extract
foreach ($data['range']['item'] as $verse)
{
	echo "<div>Book: {$verse['bookname']}, Chapter: {$verse['chapter']}, Verse: {$verse['verse']}<p>{$verse['text']}</p></div>\
";
}

// Print the structure of the entire array .... for your reference
echo "<pre>";
print_r($data);
echo "</pre>";
?>

Thanks bro, it works fine, looks like you are an expert on json.

JSON is rapidly becoming a standard way to transmit key/value data over the web, so support for it has been growing in PHP and other languages.

Up until PHP had the json functions, I always made my own custom XML parser to transform XML to arrays … my life was made infinitely easier when I discovered those two functions. Not only is it an easy way to transform XML, but it will do the same for objects and possibly other formats.

Don’t do this, it a waste of time and effort. You were very close originally, see the comments within your code below.


// 1. No need to urlencode() the whole URL, in fact this breaks everything.
$apiurl = urlencode("http://api.preachingcentral.com/bible.php?passage=$passage&version=$version");
// 2. No need to file_get_contents() as SimpleXML can read URLs.
//    In fact the 'true' says you are passing in a URL.
$data = file_get_contents('$apiurl');
$bible = new SimpleXMLElement($data,0,true);
$dtext=array();
// 3. Here $bible is the <bible> XML element.
//    Looping over $bible will loop over only its immediate children.
//    These are the <title>, <range>, <cache> and <time> elements.
//    You want to loop over the <item> elements which are held
//    within the <range> element, within the <bible> element.
foreach ($bible as $item) 
{ 
  $dtext[]=$item->text;
 }

Here is a similar script doing what you want, that works; and without the crazy JSON encode/decode.


// Properly build the URL, only encoding the query string.
$parameters = array("passage" => $passage, "version" => $version);
$apiurl     = "http://api.preachingcentral.com/bible.php?" . http_build_query($parameters);

// Fetch the XML from $apiurl.
$bible = new SimpleXMLElement($apiurl, null, TRUE);

$dtext = array();
foreach ($bible->range->item as $item) {
    $dtext[] = (string) $item->text;
}

See this example running online.