| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| NamePros Regular | 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 Code: |
| |
| | #2 (permalink) |
| NamePros Regular | 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/n...2.zip?download Then create a PHP file: PHP Code: PHP Code: PHP Code:
__________________ -Beaver6813.com V5 Soon! |
| |
| | #3 (permalink) |
| NamePros Regular | 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);
?>
|
| |
| | #4 (permalink) |
| NamePros Regular | 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. |
| |
| | #7 (permalink) | |
| NamePros Regular | Quote:
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>';
}
?>
http://www.beaver6813.com/temp/TESTNAMEPROS.php The words.txt contains: Code: workexperience pakaging
__________________ -Beaver6813.com V5 Soon! | |
| |
| | #8 (permalink) |
| NamePros Regular | 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 Code: No suggestions. Code: Did you mean: work experience Either of you please help. I really need this feature. |
| |
| | #9 (permalink) |
| Stud Sausage | 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? |
| |
| | #10 (permalink) |
| NamePros Regular | 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
__________________ -Beaver6813.com V5 Soon! |
| |
| | #11 (permalink) | |
| Stud Sausage | Quote:
| |
| |
| | #12 (permalink) | ||
| NamePros Regular | Quote:
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
__________________ -Beaver6813.com V5 Soon! | ||
| |
| | #13 (permalink) |
| NamePros Regular | 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. 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. |
| |
| | #14 (permalink) |
| NamePros Regular | I think i forgot the urlencode Code: $file = file_get_contents(urlencode("http://www.google.com/search?q=".$query));
![]() 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>';
}
?>
__________________ -Beaver6813.com V5 Soon! |
| |
| | #15 (permalink) |
| NamePros Regular | 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) I. I'm using the previous code on message #7: I put this on the text file: Code: workexperience Code: Did you mean: work experience Code: workexperience pakaging Code: No suggestions. Code: good norning 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. text file: Code: workexperience 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. Code: workexperience pakaging 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. Code: good norning 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. |
| |
| | #16 (permalink) |
| NamePros Regular | 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>';
}
?>
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 My words.txt is currently:Code: workexperience pakaging good norning
__________________ -Beaver6813.com V5 Soon! |
| |
| | #17 (permalink) |
| NamePros Regular | 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. |
| |
| | #18 (permalink) | |
| NamePros Regular | Quote:
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>';
}
?>
Its now $explodeit = explode(" ",$file); instead of $explodeit = explode("\r",$file);
__________________ -Beaver6813.com V5 Soon! | |
| |
| | #19 (permalink) |
| NamePros Regular | 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. |
| |
| | #20 (permalink) | |
| NamePros Regular | Quote:
__________________ -Beaver6813.com V5 Soon! | |
| |
| | #21 (permalink) | |
| Stud Sausage | Quote:
PHP Code: ![]() Matt. | |
| |
| | #24 (permalink) |
| Account Closed | I thought I could make a small addition myself but I'm having no luck. Maybe someone can help. How would I edit this script so that it displays the original searched term beside the suggestion? This way when working with a longer list I know what to compare. Thanks. |
| |
| | #25 (permalink) | |
| I came first! | Quote:
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 $value. " - " .getGoogleSuggestions($query).'<br>';
}
?>
__________________ █ AC/DC Talk - Talk about one of the greatest bands today. █ ChickenSMS - Let your visitors send free text messages. █ Blow Up Your Video - Read all about AC/DC and its members. █ No Cry Server - For gaming server admins. | |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |