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>";
}

    Anybody there please help me. I need to fix it urgent.

      that's a lot of code to expect people to go through\understand, can't you narrow down the issue

        try adding urlencode:

          $url = "http://suggestqueries.google.com/complete/search?qu=" . urlencode($q);
        
          dagon;10972272 wrote:

          that's a lot of code to expect people to go through\understand, can't you narrow down the issue

          something wrong in the code below..

          $q = strtolower($_GET["q"]);
          if (!$q) return;
          
          $url="http://suggestqueries.google.com/complete/search?qu=".$q;
          $text = file_get_contents($url); //Get content from Google suggest
          $text=str_replace("window.google.ac.h([\"$q\",[[","",$text); //Remove unwanted portion
          $arr_items=explode("],[",$text); //Split and put it in arrary
          foreach($arr_items as $items)
          {            $arr_item=explode(",",$items);
                      $key=$arr_item[0]; //Get the keyword, the arrary will have other details such as no.of resutls also.
                      $key=trim($key,"\""); //Use to remove quotes
                  if (strpos(strtolower($key), $q) !== false) {
                      echo "$key\n";
                  }
          
          }
            dagon;10972274 wrote:

            try adding urlencode:

              $url = "http://suggestqueries.google.com/complete/search?qu=" . urlencode($q);
            

            wow its working now thank you sooooooooooooooo much you are nice!

              hi one more things need to be fixed. please check image, if no result come then its show some unwanted word. can you please help?

                depending on what you want to do if there are no matches yo could do this:

                //...
                
                 $text = str_replace('window.google.ac.h(["' . $q . '",[[', '', $text);
                
                        if (substr($text,0,18)=='window.google.ac.h') {
                		$text	='"NO MATCHES FOR YOUR SEARCH","",""]';
                        }
                //...

                  thank you soo much . its working love you.

                    Write a Reply...