Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 977
  • Last Modified:

How to markup XML passed in a URL - Petfinder API

Hello,

I am working with the Petfinder API to create a tool that will allow people to search for pets. The way that this API works is that you generate a URL with your specific API key and some variables. The string looks like such:

http://api.petfinder.com/pet.find?key=12345&location=reno,nv

With a proper API key, this URL will return an XML page that has a ton of information about animals that meet the criteria. In the case above, it would show every animal near the location of Reno, NV.

What I'm trying to figure out is how I can pass the URL that returns all of the XML code to an HTML or PHP page that I can then markup. Basically, I want to use the information in the XML to populate a page with information and pictures. Attached is an example of what the XML code looks like. Any help is much appreciated or if someone can point me in the direction of a website that explains how to do this I would be grateful!




- <pets>
- <pet>
  <id>16966565</id> 
  <shelterId>NV83</shelterId> 
  <shelterPetId /> 
  <name>Megan</name> 
  <animal>Dog</animal> 
- <breeds>
  <breed>Australian Shepherd</breed> 
  <breed>Border Collie</breed> 
  </breeds>
  <mix>yes</mix> 
  <age>Adult</age> 
  <sex>F</sex> 
  <size>M</size> 
- <options>
  <option>altered</option> 
  <option>hasShots</option> 
  </options>
- <description>
- <![CDATA[ 
<div><p>Megan is a real character. She is an aussie border collie mix. She has been deaf her whole life but you&#39;d never know it. megan loves everyone and would make a great companion.<p><em><strong> If you are interested in this dog, please visit <a href="http://aussiebcrescue.com/adoptapp.htm" onclick="pageTracker._trackPageview(&#39;outbound/petnoteslinks&#39;);" target="_blank"> www.aussiebcrescue.com/adoptapp.htm</a> to submit an adoption application.</strong></em><br /><br /><p><em><strong> We have many wonderful Aussies and Border Collies awaiting adoptive homes. Please visit our website <a href="http://aussiebcrescue.com/index.htm" onclick="pageTracker._trackPageview(&#39;outbound/petnoteslinks&#39;);" target="_blank"> www.AussieBCrescue.com</a> to view all of our available dogs.</strong></em></div>
<br/><a href="http://www.petfinder.com/petdetail/16966565">View this pet on Petfinder.com</a>


  ]]> 
  </description>
  <lastUpdate>2010-12-04T04:37:44Z</lastUpdate> 
  <status>A</status> 
- <media>
- <photos>
  <photo id="1" size="x">http://photocache.petfinder.com/fotos/NV83/NV83.16966565-1-x.jpg</photo> 
  <photo id="1" size="t">http://photocache.petfinder.com/fotos/NV83/NV83.16966565-1-t.jpg</photo> 
  <photo id="1" size="pn">http://photocache.petfinder.com/fotos/NV83/NV83.16966565-1-pn.jpg</photo> 
  <photo id="1" size="pnt">http://photocache.petfinder.com/fotos/NV83/NV83.16966565-1-pnt.jpg</photo> 
  <photo id="1" size="fpm">http://photocache.petfinder.com/fotos/NV83/NV83.16966565-1-fpm.jpg</photo> 
  </photos>
  </media>
  </pet>

Open in new window

0
ambientsbs
Asked:
ambientsbs
  • 2
  • 2
1 Solution
 
Rik-LeggerCommented:
U can use cUrl to fetch the xml in  PHP and then use simplexml_load_string to parse the xml into an xml object.
0
 
ambientsbsAuthor Commented:
Thank you Rik-Legger.....can you be a little more specific as to how I would do this or point me in the direction of a document that explains it? I've never done it before but I'm sure I can learn how with the proper documentation or examples. Thanks!
0
 
Rik-LeggerCommented:
This should help you out some more:

	// Fetch xml via cUrl
	$oCUrl = curl_init();
	curl_setopt($oCUrl, CURLOPT_HEADER, 0);
	curl_setopt($oCUrl, CURLOPT_TIMEOUT, 5); // Timeout value
	curl_setopt($oCUrl, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
	curl_setopt($oCUrl, CURLOPT_URL, 'http://api.petfinder.com/pet.find?key=12345&location=reno,nv');
	$sOutput = curl_exec($oCUrl);
	curl_close($oCUrl);

	// Convert output to xml object
	$objXml = simplexml_load_string($sOutput);
        echo '<pre>'.print_r($objXml,1).'</pre>
        foreach ($objXml->pets->pet as $aPet) {
                echo $aPet->id . '<br />;
        }

Open in new window

0
 
ambientsbsAuthor Commented:
Okay, after further reviewing what I'd like to do here, I'm not sure that PHP is necessary and looks like it's getting a little more complicated than I would like......

Rather than using PHP, what I'd like to do is create an XSLT page with an XML stylesheet and then pass a URL to that XSLT file based on a couple of options that I give people. I think I'm going to close this question and create a separate question that's specific to XSLT. Thank you for the help, I'll definitely award points for your assistance.....
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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