Google search of website using php form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spyordie007
Forum Newbie
Posts: 4
Joined: Mon Oct 27, 2003 6:17 pm
Location: Portland, OR; USA

Google search of website using php form

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

From the source... Might be interesting.
http://cvs.php.net/co.php/phpweb/search.php
spyordie007
Forum Newbie
Posts: 4
Joined: Mon Oct 27, 2003 6:17 pm
Location: Portland, OR; USA

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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. ;)
spyordie007
Forum Newbie
Posts: 4
Joined: Mon Oct 27, 2003 6:17 pm
Location: Portland, OR; USA

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Welcome.
Post Reply