Created
March 11, 2011 06:33
-
-
Save greenido/865542 to your computer and use it in GitHub Desktop.
How To Deal With High Gear Media API Using PHP & XPath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Ido Green | |
* @since 04/10/2010 | |
* @abstract Simple example how to fetch High Gear Media Full API using xPath | |
* @see http://greenido.wordpress.com/2010/04/15/how-to-deal-with-high-gear-media-api-using-xpath/ | |
* @copyright HighGearMedia INC. 2010 | |
*/error_reporting(E_ALL); // Always good to have it in development mode | |
$feed = new DOMDocument(); | |
$feed->load('http://www.thecarconnection.com/api?uid=YourApiUserKey&cat=bottom-line'); | |
if (isset($feed->documentElement)) { | |
$xpath = new DOMXPath($feed); | |
// Lets register TheCarConnection namespace | |
$xpath->registerNamespace('tcc', 'http://www.thecarconnection.com/rss'); | |
// Now we looping on all the items and for each one we are extracting some data | |
foreach ($xpath->evaluate('//item') as $entryNode) { | |
// Simple way to get the title,desc per item | |
echo "Title: " . $xpath->evaluate('string(title)', $entryNode), "\n"; | |
echo "Description " . $xpath->evaluate('string(description)', $entryNode), "\n"; | |
// and some more interesting capabilities of xPath - we fetching the spec that is under tcc namespace for car details | |
echo "Make: " . $xpath->evaluate('string(tcc:review[1]/tcc:cardetails[1]/tcc:make[1])', $entryNode), "\n"; | |
echo "Model: " . $xpath->evaluate('string(tcc:review[1]/tcc:cardetails[1]/tcc:make[1])', $entryNode), "\n"; | |
echo "Rating: " . $xpath->evaluate('string(tcc:review[1]/tcc:cardetails[1]/tcc:rating[1])', $entryNode), " Out of 10\n"; | |
echo "============\n"; | |
} | |
} else { | |
echo 'Error: Could not fetch the feed'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment