header('Content-type: text/html; charset=UTF-8');
$g_key = 'YOUR_DEVELOPER_KEY_HERE';
$g_q = '';
if ( isset($_GET['q']) ) {
$g_q = ( isset($_GET['q']) ) ? $_GET['q'] : '';
}
$g_q = stripslashes($g_q);
?>
Google Base API Sample
if ($g_q != '') {
echo '';
} ?>
Google Base API Sample
if ($g_q != '') { showResults($g_q); } ?>
function showResults($q)
{
global $g_key;
$baseUrl = 'http://base.google.com/base/';
$url = $baseUrl . 'feeds/snippets/?q=' . urlencode($q) . '&max-results=15&key=' . urlencode($g_key);
$xml = getFileText_external($url); // My server requires a weird workaround
$xml = str_replace("xmlns='http://www.w3.org/2005/Atom'", '', $xml); // This workaround was needed as well
$dom = new domdocument;
$dom->loadXML($xml);
$xpath = new domxpath($dom);
$nodes = $xpath->query('//entry');
echo '';
foreach ($nodes as $node) {
echo '';
echo getSubNodeData($xpath, $node, 'title') . '
';
echo 'Published: ' . getSubNodeData($xpath, $node, 'published') . '
';
$itemUrl = '';
$subNodes = $xpath->query('link[@rel="alternate"][1]', $node);
$itemUrl = $subNodes->item(0)->getAttribute('href');
echo '' .
misc_toXml( niceUrl($itemUrl) ) . '
';
echo '
';
}
echo '
';
}
function niceUrl($url)
{
$maxLen = 70;
$url = str_replace('http://', '', $url);
if ( strlen($url) > $maxLen ) {
$url = substr($url, 0, $maxLen) . '...';
}
return $url;
}
function getSubNodeData($xpath, $node, $name)
{
$s = '';
$nodes = $xpath->query('.//' . $name . '[1]', $node);
foreach ($nodes as $node) {
$s = $node->firstChild->data;
break;
}
return $s;
}
function misc_toXml($s)
{
$s = str_replace('&', '&', $s);
$s = str_replace('<', '<', $s);
$s = str_replace('>', '>', $s);
return $s;
}
function misc_toAttribute($s)
{
$s = misc_toXml($s);
$s = str_replace('"', '"', $s);
return $s;
}
function getFileText_external($filePath)
{
$filetext = '';
if ( strpos($filePath, '://') !== false ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $filePath);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$filetext = curl_exec($ch);
curl_close($ch);
}
return $filetext;
}
?>