Using PHP and for some reason can not get any results to show up using SimpleXML, do I need to use some sort of namespace code to get those results parsed?


krudman

Not sure where you are having trouble, but we took the example page you posted elsewhere and got it to work by making two changes:

1. We added a specific user_agent string in the header and
2. We modified the foreach loop to remove the searchResults container. You can iterate through the xml by referring to searchListings->searchListing...

<?php
//ini_set ('allow_url_fopen', '1');
########################################
# BUILDASEARCH XML | PHP SEARCH SAMPLE
# EXAMPLE USING SIMPLEXML V.2.0.0
# MODIFIED 01182012
########################################
$key = ' ... ';//API KEY
$e = trim($_GET['e']);//QUERY STRING
$zip = 78597;//
//PARAMETERS
$START = 1;
$NUM = 2;

if(empty($e)){

echo '

';

} else {
ini_set('user_agent', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3');
//BUILDASEARCH API URL
//$API = 'http://bas.buildasearch.com/'.$name.'?e='.$e.'&key='.$key.'&format=xml&bascount=10&bastart=0';
$API = 'http://api2.yp.com/listings/v1/search?searchloc='.$zip.'&term='.$e.'&format=xml&sort=distance&radius=5&listingcount=10&key='.$key.'';

echo $API;

//LOAD EXTERNAL BAS API
$xml = simplexml_load_file($API, 'SimpleXMLElement',LIBXML_NOCDATA);

/*
//DISPLAY TOTAL
$result = $xml->xpath('/response/results/bastotal');
while(list( , $node) = each($result)) {
echo 'total: ',$node,"";
}*/

//DISPLAY RECORDS BELOW
//foreach ($xml->results->basresult as $record) {
foreach ($xml->searchListings->searchListing as $record) {
//echo 'trackingRequestURL.'">'.$record->businessName.'';
echo $record->businessName;

//echo $record->basummary.'';
//echo $record->showurl.'';
echo '';
}
}
?>

Image CAPTCHA
Facebook Twitter Sharethis