PhpDig.net

Go Back   PhpDig.net > PhpDig Forums > Mod Requests

Reply
 
Thread Tools
Old 12-13-2005, 08: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
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Google Free Online Keywords Finder grassmonster IPs, SEs, & UAs 0 07-17-2007 04:04 AM
keywords duplicates and unwanted keywords jerrywin5 How-to Forum 5 04-06-2005 04:20 PM
Cron-related mismatch type Charter Bug Tracker 0 01-03-2005 08:57 AM
Indexing errors: related to robot_functions.php Canadian Troubleshooting 2 12-31-2003 11:24 PM
2 Linux-related articles from today's NY Times maggiemel The Mole Hole 0 08-05-2003 05:14 AM


All times are GMT -8. The time now is 07:45 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.