View Single Post
Old 12-13-2005, 07:42 PM   #1
nealw
Green Mole
 
Join Date: Dec 2005
Location: Indianapolis, IN
Posts: 4
Question Google Related Keywords on searches

I am trying to figure out a way to add the php class onto the phpdig search result pages. This will pull related keywords for a search from google.

Here is a link to the script on my site:
http://www.caribbeanmag.com/phpdig/n...beach%20resort

What's the best way to integrate this?


PHP Code:
<?php
//
// Copyright (c) 2004 by Adam Stiles, adam@stilesoft.com.
// Licensed under the GNU General Public License.
// See: http://www.gnu.org/licenses/gpl.txt
//
// Class SuggestGetter. Simple class which parsers the results
// of a Google Suggest query 
//
//------------------------------------------------------------------------------
class SuggestGetter
{
//--------------------------------------------------------------------------
function GetSuggest($keyword)
{
$items = array();
$url $this->buildSuggestUrl($keyword);
$data $this->getRawSuggest($url);
if (
$data) {
//
// This parsing code could probably be a lot simpler if we used regex,
// but that's not my strong point, so just do a bruteforce parse here.
// 
$data strstr($data'new Array(');
if (
$data === FALSE) return $items;

//
// Trim off the 'new Array('
//
$data substr($datastrlen('new Array(')); 
$i strpos($data')');
if (
$i === FALSE) return $items;
$tmp substr($data0$i 1);
$tmp str_replace('"'""$tmp);
$keys explode(","$tmp);

//
// skip to next array
// TODO: this should be refactored... similar to code above
//
$data strstr($data'new Array(');
if (
$data === FALSE) return $items;
$data substr($datastrlen('new Array(')); 
$i strpos($data')');
if (
$i === FALSE) return $items;
$tmp substr($data0$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);
}

//--------------------------------------------------------------------------
//
// Grab the raw data from Google Suggest. Uses curl, but could easily be mod'ed
// to use another method, raw sockets, etc.
//
function getRawSuggest($url)
{
$ch curl_init();
curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_HEADERFALSE);
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
$data curl_exec($ch);
curl_close($ch);
if (!(
$data === FALSE)) {
return 
$data;
} else {
return 
false;

}

//--------------------------------------------------------------------------
//
// From public domain comment found on php.net:
// http://www.php.net/manual/en/function.array-combine.php
// by m71 at free dot net, 22-Oct-2004, 06:39
//
function safeArrayCombine($a$b
{
if (
count($a)!=count($b)) return FALSE;
foreach(
$a as $key) list(,$c[$key])=each($b);
return 
$c;
}
//-------------------------------------------------------------------------- 
}
//------------------------------------------------------------------------------
//
// Callback used to strip quotes from an array
//
function stripQuotes($item)
{
return 
str_replace('"'""$item);
}
//------------------------------------------------------------------------------


$sg = new SuggestGetter();
$returnedArray $sg->GetSuggest($keyword);
//print_r($returnedArray); 


foreach($returnedArray as $key => $value)
{
   
$word ltrim($key);
   
$word str_replace(' ''+',$key);
   echo 
"<a href=\"http://www.mysite.com/phpdig/search.php?query_string=$word\">$key</a><br>";
}


?>

Original code is from here: http://www.netcaptor.net/adsense/suggest.php
nealw is offline   Reply With Quote