0

First, I am pretty clueless with PHP, so be kind! I am working on a site for an SPCA (I'm a Vet and a part time geek). The PHP accesses an xml file from a portal used to administer the shelter and store images, info. The file writes that xml data to JSON and then I use the JSON data in a handlebars template, etc. I am having a problem getting some data from the xml file to outprint to JSON.

The xml file is like this:

 </DataFeedAnimal>    
<AdditionalPhotoUrls>
<string>doc_73737.jpg</string>
 <string>doc_74483.jpg</string>
 <string>doc_74484.jpg</string>
 </AdditionalPhotoUrls>
 <PrimaryPhotoUrl>19427.jpg</PrimaryPhotoUrl>
 <Sex>Male</Sex>
 <Type>Cat</Type>
 <YouTubeVideoUrls>
 <string>http://www.youtube.com/watch?v=6EMT2s4n6Xc</string>    
 </YouTubeVideoUrls>
 </DataFeedAnimal>

In the PHP file, written by a friend, the code is below, (just part of it), to access that XML data and write it to JSON:

<?php

  $url = "http://eastbayspcapets.shelterbuddy.com/DataFeeds/AnimalsForAdoption.aspx";
if ($_GET["type"] == "found") {
$url = "http://eastbayspcapets.shelterbuddy.com/DataFeeds/foundanimals.aspx";
} else if ($_GET["type"] == "lost") {
$url = "http://eastbayspcapets.shelterbuddy.com/DataFeeds/lostanimals.aspx";
 }

$response_xml_data = file_get_contents($url);
$xml = simplexml_load_string($response_xml_data);
$data = array();

 foreach($xml->DataFeedAnimal as $animal)
{
$item = array();
$item['sex'] = (string)$animal->Sex;
$item['photo'] = (string)$animal->PrimaryPhotoUrl;
$item['videos'][] = (string)$animal->YouTubeVideoUrls;
$item['photos'][] = (string)$animal->PrimaryPhotoUrl;
foreach($animal->AdditionalPhotoUrls->string as $photo) {
        $item['photos'][] = (string)$photo;
    }

$item['videos'] = array();
$data[] = $item;
}

echo file_put_contents('../adopt.json', json_encode($data));
echo json_encode($data);
?>

The JSON output works well but I am unable to get 'videos' to write out to the JSON file as the 'photos' do. I just get '/n'!

Since the friend who helped with this is no longer around, I am stuck. I have tried similar code to the foreach statement for photos but am getting nowhere. Any help would be appreciated and the pets would appreciate it as well!

Macsupport
  • 5,406
  • 4
  • 29
  • 45

2 Answers2

1

The trick with such implementations is to always look what you have got by dumping data structures to a log file or command line. Then to take a look at the documentation of the data you see. That way you know exactly what data you are working with and how to work with it ;-)

Here it turns out that the video URLs you are interested in are placed inside an object of type SimpleXMLElement with public properties, which is not really surprising if you look at the xml structure. The documentation of class SimpleXMLElement shows the method children() which iterates through all children. Just what we are looking for...

That means a clean implementation to access those sets should go along these lines:

foreach($animal->AdditionalPhotoUrls->children() as $photo) {
  $item['photos'][] = (string)$photo;
}
foreach($animal->YouTubeVideoUrls->children() as $video) {
  $item['videos'][] = (string)$video;
}

Take a look at this full and working example:

<?php
$response_xml_data = <<< EOT
<DataFeedAnimal>
  <AdditionalPhotoUrls>
    <string>doc_73737.jpg</string>
    <string>doc_74483.jpg</string>
    <string>doc_74484.jpg</string>
  </AdditionalPhotoUrls>
  <PrimaryPhotoUrl>19427.jpg</PrimaryPhotoUrl>
  <Sex>Male</Sex>
  <Type>Cat</Type>
  <YouTubeVideoUrls>
    <string>http://www.youtube.com/watch?v=6EMT2s4n6Xc</string>
    <string>http://www.youtube.com/watch?v=hgfg83mKFnd</string>
  </YouTubeVideoUrls>
</DataFeedAnimal>
EOT;

$animal = simplexml_load_string($response_xml_data);

$item = [];
$item['sex'] = (string)$animal->Sex;
$item['photo'] = (string)$animal->PrimaryPhotoUrl;
$item['photos'][] = (string)$animal->PrimaryPhotoUrl;
foreach($animal->AdditionalPhotoUrls->children() as $photo) {
  $item['photos'][] = (string)$photo;
}
$item['videos'] = [];
foreach($animal->YouTubeVideoUrls->children() as $video) {
  $item['videos'][] = (string)$video;
}

echo json_encode($item);

The obvious output of this is:

{
  "sex":"Male",
  "photo":"19427.jpg",
  "photos" ["19427.jpg","doc_73737.jpg","doc_74483.jpg","doc_74484.jpg"],
  "videos":["http:\/\/www.youtube.com\/watch?v=6EMT2s4n6Xc","http:\/\/www.youtube.com\/watch?v=hgfg83mKFnd"]
}

I would however like to add a short hint:

In m eyes it is questionable to convert such structured information into an associative array. Why? Why not a simple json_encode($animal)? The structure is perfectly fine and should be easy to work with! The output of that would be:

{
  "AdditionalPhotoUrls":{
    "string":[
      "doc_73737.jpg",
      "doc_74483.jpg",
      "doc_74484.jpg"
    ]
  },
  "PrimaryPhotoUrl":"19427.jpg",
  "Sex":"Male",
  "Type":"Cat",
  "YouTubeVideoUrls":{
    "string":[
      "http:\/\/www.youtube.com\/watch?v=6EMT2s4n6Xc",
      "http:\/\/www.youtube.com\/watch?v=hgfg83mKFnd"
    ]
  }
}

That structure describes objects (items with an inner structure, enclosed in json by {...}), not just arbitrary arrays (sets without a structure, enclosed in json by a [...]). Arrays are only used for the two unstructured sets of strings in there: photos and videos. This is much more logical, once you think about it...

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Thanks! I will try your suggestion of `json_encode($animal)` . I appreciate the time to answer my question! Works well!. The page I'm working on takes this json data and then I precompile a handlebars template to load the current pets available for adoption. I keep the json data current by running a cron job hourly on the php file. Here is a link to the demo page if you might be interested. [demo](http://yourvets.com/docs/cardsJ.html) – Macsupport Dec 07 '16 at 06:44
  • @Macsupport A beautiful web site, no question there. Congratulations! I myself do not fancy the idea of having a pet animal, I oppose it and it makes me sad to see all those animal sanctuaries filled with sad creatures. Things are certainly different for very specific tasks or for people living far out in the country side. But for the vast majority of people I personally think avoiding that habit is an overall positive attitude. – arkascha Dec 07 '16 at 09:49
0

Assuming the XML Data and the JSON data are intended to have the same structure. I would take a look at this: PHP convert XML to JSON

You may not need for loops at all.

Community
  • 1
  • 1
Jason Lemrond
  • 407
  • 2
  • 7