PDA

View Full Version : Google Related Keywords on searches


nealw
12-13-2005, 08:42 PM
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/netcaptor.php?keyword=jolly%20beach%20resort

What's the best way to integrate this?



<?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($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);

//
// 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($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);
}

//--------------------------------------------------------------------------
//
// 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($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;
}
}

//--------------------------------------------------------------------------
//
// 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