Dynadot โ€” .com Registration $8.99

[Resolved] I need Php code for bulk Google "did you mean: ..." results

Spaceship Spaceship
Watch
Impact
115
I need php code for bulk google "did you mean: ..." results

On google when I type workexperience google says "did you mean: work experience". Or type studenthomes and google will say "Did you mean: student homes".

This is a perfect feature. I want to use this feature in bulk. Do you think you can help me on this?

If it makes your job easier, some time ago Dan had written a php code for bulk google searches to count the number of search results. Maybe you can convert this code to show the words after "did you mean:" part.

PHP:
<?php

$file = file('path/to/your/list/of/words/to/search/for.txt');

$results = '';
foreach ($file as $line) {
	$data     = file_get_contents("http://www.google.com/search?hl=en&q=" . urlencode($line) . "&btnG=Google+Search");
	preg_match('/of about <b>([0-9,]*)?<\/b>/si', $data, $number);
	$results .= $line . " " . $number[1] . "\n";
}
echo $results;

?>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Do you have a Google SOAP API key for your site yet?
If you do, then you could use nusoap and SOAP to tap into google's built in spell-checker:

Download Nusoap Toolkit, extract just nusoap.php from inside the archive (its in the lib folder of the archive)
http://prdownloads.sourceforge.net/nusoap/nusoap-0.7.2.zip?download

Then create a PHP file:
PHP:
// put your developer's key here:
$key = 'INSERT GOOGLE API DEVELOPERS KEY';

include ('nusoap.php');

$soapclient = new soapclient('http://api.google.com/search/beta2');
$soapoptions = 'urn:GoogleSearch';

And below that, if you want to do a spelling suggestion:
PHP:
function do_spell( $q, $key, &$spell )
{
    global $soapclient;
    global $soapoptions;
    
    $params = array(
                'key' => $key, 
                'phrase' => $q, 
        );

    $spell = $soapclient->call('doSpellingSuggestion', $params, $soapoptions);

    $err = $soapclient->getError();

    if ($err)
    {
        print("<br>An error occurred!<br>");
        print(" Error: $err<br>\n");
        return false;
    }

    return true;
}

Wherever you want the script to check you just do:
PHP:
$Query = 'workexperience'; //Put search term here
do_spell($Query, $key, $spell);
          if ($spell[0]) {
            print "Did you mean <b><a href=\"../search/?q=".$spell."\">".$spell."</a></b>? ";
          }
 
1
•••
Or.... you can use this:
(Just written)

Code:
<?
function getGoogleSuggestions($query){
	$file = file_get_contents("http://www.google.com/search?q=".$query);
	if(!eregi("Did you mean", $file)){
		return "No suggestions.";
	}else{
		$ex0 = explode('Did you mean:', $file);
		$ex1 = str_replace('</i></b>', '<b><i>', $ex0[1]);
		$ex2 = explode("<b><i>", $ex1);
		return "Did you mean: ".$ex2[1];
	}
}

$query = "studentexperience";
echo getGoogleSuggestions($query);
?>

(Feel free to leave rep or NP)
 
1
•••
beaver6813,
These are too complicated for me. I'm just an average computer user. Thank you anyway for your great effort.

cvxdes,
I think this is closer to what I need but can you make the code so that it reads the input list from a text file. For instance on my first message you see Dan's code. There is this line:
$file = file('path/to/your/list/of/words/to/search/for.txt');

I'm using this method. What happens is that I put 400 words on my data file. I have different php files for different search types like this:

bulksearch.php
picturesearch.php
frooglesearch.php
inurlsearch.php

All these searches get the data from the same text file. So leaving the text file at the same place I click on different php files. Now I want to add one didyoumean.php file to that list. It should read the same text file like dan's code.
 
0
•••
Is the text file comma seperated or is it seperated by lines... or what is it seperated by?
 
1
•••
Hi cvxdes,
It is seperated by lines.
 
0
•••
MarcelProust said:
Hi cvxdes,
It is seperated by lines.
Hey,
Modified cvxdes's code :)

Code:
<?
function getGoogleSuggestions($query){

	$file = file_get_contents("http://www.google.com/search?q=".$query);
	if(!eregi("Did you mean", $file)){
		return "No suggestions.";
	}else{
		$ex0 = explode('Did you mean:', $file);
		$ex1 = str_replace('</i></b>', '<b><i>', $ex0[1]);
		$ex2 = explode("<b><i>", $ex1);
		return "Did you mean: ".$ex2[1];
	}
}
$file = file_get_contents('words.txt');
$explodeit = explode("\r",$file);
foreach($explodeit as $value) {
$query = trim($value);
echo getGoogleSuggestions($query).'<br>';
}
?>

That will get all words from the file words.txt and go through each line checking, then return all suggestions, example:
http://www.beaver6813.com/temp/TESTNAMEPROS.php

The words.txt contains:
Code:
workexperience
pakaging
 
0
•••
Hello beaver6813,

Thank you for your effort. It was very nice of you to show a working example of it. On the other hand when I try there is a small problem. Only when the text file has one line then it works. Otherwise it doesn't. For instance when I insert our example keywords,
Code:
workexperience
pakaging
it says
Code:
No suggestions.
Only when I remove the second line on the text file and leave the first line the it says
Code:
Did you mean: work experience

I don't know what I'm doing different than you. Are you sure you have copied the php code from your working file?

Either of you please help. I really need this feature.
 
0
•••
First of all this is a terrible way to do it lol. This is going to kill your pages execution time, wouldn't be surprised it increased it x4 or more. Atthe very least use curl instead of file_get_contents which is extremely slower.

beaver6813's initial suggestion of using the Google API is far, far better and is not actually that complicated.


Anyway, MarcelProust i have tried the code given and it works fine.

Could you show us how you are using it?
 
0
•••
Heh, it doesn't really matter which way he does it, it'll be hard for him to get a SOAP api key anyways for google now because they've stopped officially supporting it, they're trying to get people to use their Ajax Search instead which is quite frankly too limiting and doesn't have spell check.

I don't know why it isn't working for you... ill upload the exact file somewhere... maybe youve uploaded it wrong :S Works fine for me ;)

You should be able to download here: http://www.beaver6813.com/temp/TESTNAMEPROS.php.bak
 
0
•••
beaver6813 said:
Heh, it doesn't really matter which way he does it, it'll be hard for him to get a SOAP api key anyways for google now because they've stopped officially supporting it, they're trying to get people to use their Ajax Search instead which is quite frankly too limiting and doesn't have spell check.

Aren't the API keys the same for all services though? Talking blind here, haven't used or look at it in a couple of years :hehe:
 
0
•••
Matthew. said:
beaver6813 said:
Heh, it doesn't really matter which way he does it, it'll be hard for him to get a SOAP api key anyways for google now because they've stopped officially supporting it, they're trying to get people to use their Ajax Search instead which is quite frankly too limiting and doesn't have spell check.

Aren't the API keys the same for all services though? Talking blind here, haven't used or look at it in a couple of years :hehe:

Nah they're not :) They no longer provide SOAP API Keys for domains, only existing api keys will still work, i used a lot of search engine stuff whilst working on twerq.com so im in the know when it comes to google api lol Yahoo on the other hand are much nicer and has less limits and provide more support for their API :) Yahoo aren't evil, but google are :(
 
0
•••
Hello,
It appears its only me who can't get this working. This is somewhat strange because if there is one word one first line it works. So the php file reads the data from text file but for some reason in most cases it doesnt read correctly. I have been experimenting with this for a while.

If the first line has one word like "workexperience" and any other line is blank than it works. If I put anything on the second line, any word or even a dot than it doesnt work.

On the first line if I put "good norning" and don't put anything on other lines it doesn't work. Somehow the space between these words causes a problem. I've got this error after this try:

Code:
Warning: file_get_contents(http://www.google.com/search?q=good norning) [function.file-get-contents]: failed to open stream: HTTP request failed! in /home/.../public_html/.../domiansearch/TESTNAMEPROS.php on line 4
No suggestions.

So I go back to the text file remove the space and leave "goodnorning". Now it works again.

What am I doing wrong. On cpanel file manager I create a file and name it as filename.php. Than I click edit file and paste the code I copy from beaver6813's message. This is rather strange since my other php file bulkgooglesearch.php, work just perfect.
 
0
•••
I think i forgot the urlencode :bah:

Code:
	$file = file_get_contents(urlencode("http://www.google.com/search?q=".$query));

I've changed that line with urlencode, so queries with spaces in them should now work, although if it still doesn't work for you, pm me the cpanel details and where it is etc and ill take a look :)

The whole script should now be:
Code:
<?
function getGoogleSuggestions($query){

$file = file_get_contents(urlencode("http://www.google.com/search?q=".$query));
	if(!eregi("Did you mean", $file)){
		return "No suggestions.";
	}else{
		$ex0 = explode('Did you mean:', $file);
		$ex1 = str_replace('</i></b>', '<b><i>', $ex0[1]);
		$ex2 = explode("<b><i>", $ex1);
		return "Did you mean: ".$ex2[1];
	}
}
$file = file_get_contents('words.txt');
$explodeit = explode("\r",$file);
foreach($explodeit as $value) {
$query = trim($value);
echo getGoogleSuggestions($query).'<br>';
}
?>
 
0
•••
Hello beaver,
Thanks for your patience. Unfortunately this second code doesn't work at all. The previous code at least worked when there is only one word only on the first line. This second code did not work in any case. Is there absolutely no way to write a smillar code to Dan's which is in my first message. It works perfect for bulk search results. No matter what I put on any line it searches it on google and returns me the count.

Here is some information about my cpanel:
Code:
General server information: 
Operating system Linux 
Service Status Click to View 
Kernel version 2.6.9-42.0.3.ELsmp 
Machine Type i686 
Apache version 1.3.37 (Unix) 
PERL version 5.8.7 
Path to PERL /usr/bin/perl 
Path to sendmail /usr/sbin/sendmail 
Installed Perl Modules Click to View 
PHP version 4.4.4 
MySQL version 4.1.21-standard 
cPanel Build 10.9.0-CURRENT 117 
Theme cPanel X v2.6.0  
Documentation Click to View 
cPanel Pro 1.0 (RC36)

Let me summarize:

I. I'm using the previous code on message #7:
I put this on the text file:
Code:
workexperience
And ฤฑ get this result:
Code:
Did you mean: work experience
So it works fine. Then I put this in the text file:
Code:
workexperience
pakaging
and I get this result:
Code:
No suggestions.
Lastly I put this in the text file:
Code:
good norning
and get this result:
Code:
Warning: file_get_contents(http://www.google.com/search?q=good norning) [function.file-get-contents]: failed to open stream: HTTP request failed! in /home/.../public_html/.../domiansearch/didyoumean.php on line 4
No suggestions.

II. Now I'm using your second code on message #14.
text file:
Code:
workexperience
result:
Code:
Warning: file_get_contents(http%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dworkexperience) [function.file-get-contents]: failed to open stream: No such file or directory in /home/.../public_html/.../domiansearch/didyoumean.php on line 4
No suggestions.

Text file:
Code:
workexperience
pakaging
Result:
Code:
Warning: file_get_contents(http%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dworkexperience%0Apakaging) [function.file-get-contents]: failed to open stream: No such file or directory in /home/.../public_html/.../domiansearch/didyoumean.php on line 4
No suggestions.

Lastly, text file is
Code:
good norning
And the result is
Code:
Warning: file_get_contents(http%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dgood+norning) [function.file-get-contents]: failed to open stream: No such file or directory in /home/.../public_html/.../domiansearch/didyoumean.php on line 4
No suggestions.
 
0
•••
Right this has gotta work lol, i've rewritten even more of the script, i don't have time to optimize it or anything at the moment so its just a really quick job, but it works :)

Code:
<?
function getGoogleSuggestions($query){

$file = file_get_contents("http://www.google.com/search?q=".urlencode($query));
	if(!eregi("Did you mean", $file)){
		return "No suggestions.";
	}else{
		$ex0 = explode('Did you mean:', $file);
		$ex2 = explode("</i></b>", $ex0[1]);
		$ex3 = explode("class=p>",$ex2[0]);
		$ex4 = str_replace('<b><i>', '', $ex3[1]);
		return "Did you mean: ".$ex4;
	}
}
$file = file_get_contents('words.txt');
$explodeit = explode("\r",$file);
foreach($explodeit as $value) {
$query = trim($value);
echo getGoogleSuggestions($query).'<br>';
}
?>

Changed:
Posistion of URLEncode so it only applies to query string.
The way the script removes all other junk from the page, i had to do this so it could retrieve the right spelling for queries with more than one word. like good morning :)
When running this script i got the response:
Code:
Did you mean: work experience
Did you mean: packaging
Did you mean: good morning

So it appears to be working :) My words.txt is currently:
Code:
workexperience
pakaging
good norning
 
0
•••
Hello beaver,

Unfortunately it doesn't work. The different lines are not recognized by the script. It reads them as if they were all on the same line.

I don't know why it works for others and not for me. At the beginning I thought this would be something very easy to solve.

On Dan's code what is different that makes it work without a problem? Again there is a text file and again it reads whatever is on each line. It doesn't matter if there are multiple words on lines or how many lines there are and it always works smooth. I think the way Dan's scrips gets the data from the text file is different than what we are trying and for some unknown reason the former works while the latter doesn't.

I'm sorry for being a troublemaker on this issue especially when the title already says "resolved" and when others have reported it works for them.
 
0
•••
MarcelProust said:
Hello beaver,

Unfortunately it doesn't work. The different lines are not recognized by the script. It reads them as if they were all on the same line.

I don't know why it works for others and not for me. At the beginning I thought this would be something very easy to solve.

On Dan's code what is different that makes it work without a problem? Again there is a text file and again it reads whatever is on each line. It doesn't matter if there are multiple words on lines or how many lines there are and it always works smooth. I think the way Dan's scrips gets the data from the text file is different than what we are trying and for some unknown reason the former works while the latter doesn't.

I'm sorry for being a troublemaker on this issue especially when the title already says "resolved" and when others have reported it works for them.

Lol i seriously don't know why its not working for you :P Okay since you said its reading it all as one line, i've changed the explode part of the script, its not as neat but it may work for your version. PM me with any IM you have to we can get this sorted faster :P

Code:
<?
function getGoogleSuggestions($query){

$file = file_get_contents("http://www.google.com/search?q=".urlencode($query));
	if(!eregi("Did you mean", $file)){
		return "No suggestions.";
	}else{
		$ex0 = explode('Did you mean:', $file);
		$ex2 = explode("</i></b>", $ex0[1]);
		$ex3 = explode("class=p>",$ex2[0]);
		$ex4 = str_replace('<b><i>', '', $ex3[1]);
		return "Did you mean: ".$ex4;
	}
}
$file = file_get_contents('words.txt');
$explodeit = explode("
",$file);
foreach($explodeit as $value) {
$query = trim($value);
echo getGoogleSuggestions($query).'<br>';
}
?>

Changed:
Its now $explodeit = explode("
",$file); instead of $explodeit = explode("\r",$file);
 
0
•••
It works, it works!
Thank you so much beaver for you great effort and for your patience on this. Now it is resolved. I've put 200 words in the text file and as I saw the results page opening 2 words per second, the list growing bigger and bigger, it was like dominos were falling. So beautiful. Repetition and some namebuck added to both of you. Thanks.
 
0
•••
MarcelProust said:
It works, it works!
Thank you so much beaver for you great effort and for your patience on this. Now it is resolved. I've put 200 words in the text file and as I saw the results page opening 2 words per second, the list growing bigger and bigger, it was like dominos were falling. So beautiful. Repetition and some namebuck added to both of you. Thanks.

All in a days work :gl:
 
0
•••
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back