Problem loading XML data using multiple queries in PHP-ThrowExceptions

Exception or error:

I have the following XML file

<response>
<query>hello</query>
<zone name="url">
    <records s="*" n="0" total="0"/>
</zone>
<zone name="people">
    <records s="*" n="20" total="58" next="/result?q=hello&zone=people&s=AoIIRib9grewqTU2MGRkMS02YzgzLTQxYQ%3D" nextStart="AoIIRib9GD8NaWRlbnRpddfsdfk0OWQ=">
        <people id="1140172" url="/people/1140172">
        </people>
        <people id="1482231" url="/people/1482231">
        </people>
        <people id="832153" url="/people/832153">
        </people>
    </records>
</zone>
<zone name="collection">
    <records s="*" n="20" total="496" next="/result?q=hello&zone=collection&s=AoIIQsdfszdTIyNzc3MDUzNg%3D%3D" nextStart="AoIIQ3sHAysdfdszc3MDUzNg==">
        <work id="208227193" url="/work/208227193">
        </work>
        <work id="225096966" url="/work/225096966">
        </work>
        <work id="225096968" url="/work/225096968">
        </work>
    </records>
</zone>
<zone name="gazette">
    <records s="*" n="20" total="91239" next="/result?q=hello&zone=gazette&s=AoIIRdAyNjk3NzQ%3D" nextStart="AoIIgdfgek3NzQ=">
        <article id="249121748" url="/newspaper/249121748">
        </article>
        <article id="225072553" url="/newspaper/225072553">
        </article>
        <article id="249121741" url="/newspaper/249121741">
        </article>
    </records>
</zone>
<zone name="book">
    <records s="*" n="20" total="9245" next="/result?q=hello&zone=book&s=AogxMjk1Mg%3D%3D" nextStart="AoIIQ6HewytzdTIjk1Mg==">
        <work id="19554432" url="/work/19554432">
        </work>
        <work id="31597077" url="/work/31597077">
        </work>
        <work id="11135964" url="/work/11135964">
        </work>
    </records>
  </zone>
</response>

I am trying to run multiple queries on the same page using the same data but it has a few glitches. Some “Zones” load and some don’t.
Here is the PHP i am using to load the data;

    if ($_SERVER["REQUEST_METHOD"] == "GET") { 
     $last_name= $_GET["lastname"];
     $first_name= $_GET["firstname"];
     $result = simplexml_load_string(file_get_contents('https://api.url/v2/result?key=e4oheeek7rrr&zone=all&q='.$first_name.'%20'.$last_name));

        $books = $result->zone[5]->records->work;
        $newspaper = $result->zone[8]->records->article;
        $collection = $result->zone[3]->records->work;
        $gazette = $result->zone[4]->records->article;
    }

Then in sections I am using the following to display the result but it doesn’t load all of the zones at once. If I run the queries separately they work just fine. Below are 2 example queiries;

   // Display XML Results for Book Zone
 $id = 0;
     foreach ($books as $book) {
echo ' <div class="col-xs-12 col-sm-12 col-md-3 col-lg-3 col-xl-3 flex-container">';
     foreach ($book->title as $titles) {
        echo "<div class='book'>";
        echo "<span class='titles'>".$titles."</span><br>";
     }

     foreach ($book->snippet as $about){
        echo "<p>Snippet : ".$about."</p>";
     }

     foreach ($book->type as $type){
        echo "<p>Type: ".$type."</p>";
     }

     foreach ($book->identifier as $links){
        echo "<p>Websites: <a href='".$links."' target='_blank' title='Opens in new window' data-toggle='tooltip'>".$links."</a></p>";
     }

     foreach ($book->contributor as $contributor){
        echo "<p>Contributor : ".$contributor."<p>";
     }

     foreach ($book->issued as $issued){
        echo "<p>Issued : ".$issued."</p>";
     }

if ($id==7){
    echo "<p>Please register to view more results</p>";
    break;
     }
$id= $id+1;

         echo '</div></div>';
   }

This is the second query. If I try to display data from 4 “zones” sometimes 1 or 2 load and it could be different “zones” that load each time.

// load data for Newspaper Zone
$z = 0;    
    foreach ($newspaper as $paper) {

    foreach ($paper->title as $papername) {
        echo "<span class='titles'>Newspaper: ".$papername."</span><br>";
     }
    foreach ($paper->heading as $heading) {
        echo "<span class='headings'> Headings: ".$heading."</span>";
     }
    foreach ($paper->snippet as $snippets){
        echo "<span class='snippet'>Snippet: ".$snippets."</span>";
     }
    foreach ($paper->troveurl as $web){
        echo "<b>Websites: </b><a href='".$web."'>".$web."</a><br>";
     }
    foreach ($paper->edition as $edition){
        echo "<b>Edition </b>: ".$edition."<br>";
     }
$z= $z+1;
if ($z==5){
    echo "Please register to view more results";
    break;
     }
   }

Thanks.

How to solve:

Use Xpath to fetch the zones by their name attributes instead their position.

$response = new SimpleXMLElement($xmlString);
$books = $response->xpath('//zone[@name="books"]/records/*');
foreach ($books as $book) {
  // ...
} 

Leave a Reply

Your email address will not be published. Required fields are marked *