i want to use ajax suggest v2 in my search field for suggest google keyword. i modified ajax suggest php file and trying to implement google keyword suggest.
now things work if i use one word like "hi" or "hello" but if i use two word then doesnt show any result like "hi col" "hello my"
please help me i want result should show continually.
google suggest keyword array
$q = strtolower($_GET["q"]);
if (!$q) return;
$len = strlen($q);
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
$aResults = array();
$count = 0; $i=0;
$url = "http://suggestqueries.google.com/complete/search?qu=" . $q;
$text = file_get_contents($url);
$text = str_replace('window.google.ac.h(["' . $q . '",[[', '', $text);
$arr_items = explode('],[', $text);
foreach($arr_items as $items) {
$arr_item = explode(',', $items);
$key = $arr_item[0];
$key = trim($key, '"');
if (strpos(strtolower($key), $q) !== false) {
$count++;
$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($key) );
}
full php code
<?php
$q = strtolower($_GET["q"]);
if (!$q) return;
$len = strlen($q);
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
$aResults = array();
$count = 0; $i=0;
$url = "http://suggestqueries.google.com/complete/search?qu=" . $q;
$text = file_get_contents($url);
$text = str_replace('window.google.ac.h(["' . $q . '",[[', '', $text);
$arr_items = explode('],[', $text);
foreach($arr_items as $items) {
$arr_item = explode(',', $items);
$key = $arr_item[0];
$key = trim($key, '"');
if (strpos(strtolower($key), $q) !== false) {
$count++;
$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($key) );
}
if ($limit && $count==$limit)
break;
$i++;
}
/* if ($len)
{
for ($i=0;$i<count($aUsers);$i++)
{
// had to use utf_decode, here
// not necessary if the results are coming from mysql
//
if (strtolower(substr(utf8_decode($aUsers[$i]),0,$len)) == $q)
{
$count++;
$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
}
if ($limit && $count==$limit)
break;
}
} */
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache"); // HTTP/1.0
if (isset($_REQUEST['json']))
{
header("Content-Type: application/json");
echo "{\"results\": [";
$arr = array();
for ($i=0;$i<count($aResults);$i++)
{
$arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"\"}";
}
echo implode(", ", $arr);
echo "]}";
}
else
{
header("Content-Type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
for ($i=0;$i<count($aResults);$i++)
{
echo "<rs id=\"".$aResults[$i]['id']."\" info=\"".$aResults[$i]['info']."\">".$aResults[$i]['value']."</rs>";
}
echo "</results>";
}