| 
	
 | 
 Posted by jason on 08/06/07 19:11 
I have a php script that I have uploaded to  
http://tangorobert.110mb.com/suggest.php?keyword=cars 
 
When you run the script, it returns results from google.  In the case of the  
above example, the results are as follows: 
 
cars 345,000,000 results Get Suggestion 
cars.com 8,670,000 results Get Suggestion 
cars for sale 40,000,000 results Get Suggestion 
carsales 303,000 results Get Suggestion 
cars guide 63,400,000 results Get Suggestion 
carsoup 667,000 results Get Suggestion 
carsguide 341,000 results Get Suggestion 
cars the movie 61,900,000 results Get Suggestion 
carson pirie scott 245,000 results Get Suggestion 
carsales.com.au 79,600 results Get Suggestion 
 
What I was wondering is how the script could be modified so that in addition  
to the above results, it would also bring back results for each of the  
individual words from the the above phrases. 
 
Hence, when the script is run, it would output the following (note the last  
9 listings): 
 
cars 345,000,000 results Get Suggestion 
cars.com 8,670,000 results Get Suggestion 
cars for sale 40,000,000 results Get Suggestion 
carsales 303,000 results Get Suggestion 
cars guide 63,400,000 results Get Suggestion 
carsoup 667,000 results Get Suggestion 
carsguide 341,000 results Get Suggestion 
cars the movie 61,900,000 results Get Suggestion 
carson pirie scott 245,000 results Get Suggestion 
carsales.com.au 79,600 results Get Suggestion 
cars xxx,xxx results Get Suggestion 
com xxx,xxx results Get Suggestion 
for xxx,xxx results Get Suggestion 
sale xxx,xxx results Get Suggestion 
guide xxx,xxx results Get Suggestion 
the xxx,xxx results Get Suggestion 
movie xxx,xxx results Get Suggestion 
pirie xxx,xxx results Get Suggestion 
scott xxx,xxx results Get Suggestion 
 
I have listed the script below.  Note there are two files, suggest.php and  
suggest_inc.php 
 
TIA 
 
Jason 
 
********************* 
suggest.php 
********************* 
 
<?php 
require("suggest_inc.php"); 
 
$suggest_items = array(); 
$show_suggest = false; 
$keyword = ""; 
$no_suggest = false; 
 
if (isset($_POST["keyword"])) { 
 $keyword = $_POST["keyword"]; 
} elseif (isset($_GET["keyword"])) { 
 $keyword = $_GET["keyword"]; 
} 
 
if ($keyword != "") { 
 $suggest = new SuggestGetter(); 
 $suggest_items = $suggest->GetSuggest($keyword); 
 if (count($suggest_items) > 0) { 
  $show_suggest = true; 
 } else { 
  $no_suggest = true; 
 } 
} 
 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Google Suggest Keyword Tool</title> 
 
</head> 
<body> 
<table width="480" height="121%" border="0" align="center" valign="middle"> 
 
  <tr> 
    <td width="100%" height="242" align="center" valign="top"><br /> 
        <table width="409" border="0" align="center" cellpadding="0"  
cellspacing="0" > 
 <tr> 
 <td width="420" height="174" align="center" valign="top"><table  
width="100%" align="center"> 
              <tr> 
                <td height="26"><form><input value="<?php echo $keyword; ?>"  
type="text" size="35" name="keyword" /> 
                  <input type="submit" value="Get Suggestion"/> 
                </form>    </td> 
            </table> 
              <?php 
  if ($show_suggest) { 
?> 
              <table align="center"> 
              <?php 
   foreach ($suggest_items as $key => $value) { 
    echo "<tr><td class='details'><a  
http://www.google.com/search?hl=en&btnG=Google+Search&q=$key'  
target='_blank'>$key</a></td><td class='details'>" . trim($value) .  
"</td><td class='details'><a href=  
'http://tangorobert.110mb.com/suggest.php?keyword=$key' target='_self'>Get  
Suggestion</a</td></tr>"; 
   } 
   echo "</table>"; 
  } 
  ?> 
  <br /> 
<br /> 
<br /> 
        </table>  </td> 
  </tr> 
      </table> 
      <br /> 
      <br /> 
 
  </table> 
</body> 
</html> 
 
****************************** 
suggest_inc.php 
****************************** 
 
<?php 
 
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);}?>
 
  
Navigation:
[Reply to this message] 
 |