Hello, can someone look at this:
Create the following page and call it suggest.php:
<?php
//
// NOTE - this page needs CURL installed on the server
// to run. It can be downloaded from http://curl.haxx.se/
// or should appear in the c:\php\ext dir - enable it in php.ini
//------------------------------------------------------------------------------
class SuggestGetter
{
//--------------------------------------------------------------------------
function GetSuggest($keyword)
{
$items = array();
$url = $this->buildSuggestUrl($keyword);
$data = $this->getRawSuggest($url);
if ($data) {
$data = strstr($data, 'new Array(');
if ($data === FALSE) return $items;
$data = substr($data, strlen('new Array('));
$i = strpos($data, ')');
if ($i === FALSE) return $items;
$tmp = substr($data, 0, $i - 1);
$tmp = str_replace('"', "", $tmp);
$keys = explode(",", $tmp);
$data = strstr($data, 'new Array(');
if ($data === FALSE) return $items;
$data = substr($data, strlen('new Array('));
$i = strpos($data, ')');
if ($i === FALSE) return $items;
$tmp = substr($data, 0, $i - 1);
$values = explode('",', $tmp);
$values = array_map("stripQuotes", $values);
if (count($keys) == count($values)) {
$items = $this->safeArrayCombine($keys, $values);
}
}
return $items;
}
//--------------------------------------------------------------------------
function buildSuggestUrl($keyword)
{
return "http://www.google.com/complete/search?hl=en&js=true&qu=" . urlencode($keyword);
}
function getRawSuggest($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
curl_close($ch);
if (!($data === FALSE)) {
return $data;
} else {
return false;
}
}
function safeArrayCombine($a, $b)
{
if (count($a)!=count($b)) return FALSE;
foreach($a as $key) list(,$c[$key])=each($b);
return $c;
}
//--------------------------------------------------------------------------
}
function stripQuotes($item)
{
return str_replace('"', "", $item);
}
function LIST_CONTENTS($arrayname,$tab="    ",$indent=0)
{
while(list($key, $value) = each($arrayname))
{
for($i=0; $i<$indent; $i++) $currenttab .= $tab;
if (is_array($value))
{
$retval .= "$currenttab$key : Array: <BR>$currenttab{<BR>";
$retval .= LIST_CONTENTS($value,$tab,$indent+1)."$currenttab}<BR>";
}
else $retval .= "$currenttab<a href=suggest.php?txtSearchTerm=$key><b>$key</b></a> <font size=1>($value)</font><BR>";
$currenttab = NULL;
}
return $retval;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Google Suggest</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.greyborder {
border: 1px dashed #999999;
}
-->
</style>
</head>
<body>
<form name="form1" method="get" action="suggest.php">
<input name="txtSearchTerm" type="text" id="txtSearchTerm">
<input type="submit" name="Submit" value="Submit">
</form>
<table width="222" border="0" cellpadding="3" cellspacing="0" bgcolor="#F5F5F5">
<tr>
<td class="greyborder"><?php
if ( $GET['txtSearchTerm'] != "" ) {
$GoogleArray = new SuggestGetter;
//print_r($test->GetSuggest("betta"));
$tabbed = LIST_CONTENTS($GoogleArray->GetSuggest($GET['txtSearchTerm']),"    ",0);
print $tabbed;
} else {
echo "No querystring detected!";
}
?>
</td>
</tr>
</table>
</body>
</html>
Now run the page and submit a keyword query such as 'goldfish', now hover over the results and notice that the link querystring is not being filled for any of list bar the first entry. Anyone know how i can get it to fill in the querystring with the value shown in the list? Thanks.
PS - You need curl enable to run this page.