Page 1 of 1

Google search of website using php form

Posted: Mon Oct 27, 2003 6:17 pm
by spyordie007
We have decided to make use of the Google search engine to search our website and have been looking at a few differant ways of doing it, the first would be the Google API and PHP/SOAP however I have too many projects on my hand to setup/test PEAR/SOAP + the API and would like to impliment something simple.

I decided it would be best to mimic the search on php.net and thought it would be simple enough to make a form and have it submit to a redirect that forms the search into a Google search url (Search+String+Site:mysite.com).

I've built my script and it will search correctly if I only input a single keyword; however spaces (" ") need to become pluses ("+") otherwise Google doesnt know what to do with multiple keywords.

All I really want is exactly what they have done on php.net, to take my entered words, seperate them by spaces and add the site:mysite.com option so it will use Google's index for items that are only part of my site. Does anyone have any reccomendations for how to finish this? Better yet has anyone ever done this before and would be willing to share their code?

Any help appreciated!

-Erik

Posted: Mon Oct 27, 2003 6:54 pm
by JAM
From the source... Might be interesting.
http://cvs.php.net/co.php/phpweb/search.php

Posted: Mon Oct 27, 2003 8:16 pm
by spyordie007
I'm sorry, I've been looking at the code from the PHP site (thanks for linking me to that JAM) but havent had any luck understanding what part of the code is responsible for this. I'm not very good at PHP and wonder if anyone here would be kind enough to spell it out for me (i.e. look at line x, copy code make your form input variable x, etc.).

Thanks again

-Erik

Posted: Mon Oct 27, 2003 8:24 pm
by JAM
Okay ;)

Former page, line 45.

Code: Select all

$ucp = urlencode($_FORM['pattern']);
They are using urlencode() to change a forms input field's non-alphanumeric characters to %XX (xx being numerical hex equilant). Space is for example %20.

Line 81, same page

Code: Select all

"http://www.google.com/search?q={$ucp}+site:www.php.net&l=$LANG"
They are adding that variable in the search condition. You can clearly see what the different parts of the above url means, and what the different variables in it might do. q: query/question, site: where, l: language.

Now go experiment, and change this aroundto a combination that would work for you. ;)

Posted: Mon Oct 27, 2003 10:45 pm
by spyordie007
Hey that was easy, pulled the variable & used urlencode on it than stored that as a new variable and submitted it in the URL.

Thanks for the help!

-Erik

Posted: Tue Oct 28, 2003 4:23 am
by JAM
Welcome.