How to markup XML passed in a URL - Petfinder API

Posted on 2011-04-19
Last Modified: 2012-05-11

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:,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>
  <shelterPetId /> 
- <breeds>
  <breed>Australian Shepherd</breed> 
  <breed>Border Collie</breed> 
- <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="" onclick="pageTracker._trackPageview(&#39;outbound/petnoteslinks&#39;);" target="_blank"></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="" onclick="pageTracker._trackPageview(&#39;outbound/petnoteslinks&#39;);" target="_blank"></a> to view all of our available dogs.</strong></em></div>
<br/><a href="">View this pet on</a>

- <media>
- <photos>
  <photo id="1" size="x"></photo> 
  <photo id="1" size="t"></photo> 
  <photo id="1" size="pn"></photo> 
  <photo id="1" size="pnt"></photo> 
  <photo id="1" size="fpm"></photo> 

Open in new window

Question by:ambientsbs
    LVL 8

    Expert Comment

    U can use cUrl to fetch the xml in  PHP and then use simplexml_load_string to parse the xml into an xml object.

    Author Comment

    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!
    LVL 8

    Accepted Solution

    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, ',nv');
    	$sOutput = curl_exec($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


    Author Closing Comment

    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.....

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now