Newbie needs some help - Google script

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. 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">

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






</table> </td>
</tr>
</table>





</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);}?>
Oliver Weichhold [ Di, 07 August 2007 05:03 ] [ ID #1788918 ]

Re: Newbie needs some help - Google script

On Aug 7, 1:03 pm, "jason" <n... [at] spam.com> wrote:
> I have a php script that I have uploaded tohttp://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:
-- SNIP --

AFAIK you can't, google return that suggestions.
Well, you can, but it'll be tedious using PHP.

basically it calls for this page: http://www.google.com/complete/search?hl=en&js=true&qu=cars

Hendri Kurniawan
Hendri Kurniawan [ Di, 07 August 2007 05:42 ] [ ID #1788919 ]

Re: Newbie needs some help - Google script

Thanks for your response, I do see that this site
http://www.suggestexplorer.com/?q=cars has done something similar to what
I'm looking for.


"Hendri Kurniawan" <hckurniawan [at] gmail.com> wrote in message
news:1186458161.013850.58110 [at] j4g2000prf.googlegroups.com...
> On Aug 7, 1:03 pm, "jason" <n... [at] spam.com> wrote:
>> I have a php script that I have uploaded
>> tohttp://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:
> -- SNIP --
>
> AFAIK you can't, google return that suggestions.
> Well, you can, but it'll be tedious using PHP.
>
> basically it calls for this page:
> http://www.google.com/complete/search?hl=en&js=true&qu=cars
>
> Hendri Kurniawan
>
>
Oliver Weichhold [ Di, 07 August 2007 05:50 ] [ ID #1788920 ]

Re: Newbie needs some help - Google script

On Aug 7, 1:50 pm, "jason" <n... [at] spam.com> wrote:
> Thanks for your response, I do see that this sitehttp://www.suggestexplorer.com/?q=carshas done something similar to what
> I'm looking for.
>
-- SNIP --
>
> > Hendri Kurniawan

unfortunately I can't open your url.
Yes it can be done, but I don't really have time to elaborate on that.
The following pseudo code *might* work:

1. Retrieve your google suggestion.
2. For each suggestion keywords, perform preg_match (or explode) to
separate each "word". ie. cars.com become array(cars, com)
3. Foreach of those keywrods, you repeatedly query to google, getting
only the first set of result
4. Display result

You should probably consider Google API instead.
(Which I'm not too familiar with, but I'm sure it's much better in
terms of performance and programming-time wise).

Hendri Kurniawan
Hendri Kurniawan [ Di, 07 August 2007 06:09 ] [ ID #1788921 ]
PHP » alt.php » Newbie needs some help - Google script

Vorheriges Thema: Free Form Builder Utility with PHP back end
Nächstes Thema: Why does it wait before echoing?