Where is my URL in a search engine????

Hi,
Here is a cool one. If I submit a search string to a search engine such as Google, how can I parse the results to find what page my URL appears on?

1.) Can it be done?
2.) How?

A realy interesting one this, Open for debate and if anyone knows, there are certainly some points availible.

Stu
08718712060Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Giovanni GCTOCommented:
Funny that, a customer asked me the same thing about one month ago. I'll tell you the same I replied to him.

It surely can be done, the mechanical is quite simple, fopen() the composed search URL (including the search string), and parse the result probably with a regexp, counting the results (they are usually created from a fixed template, so you should be able to count the results before yours).

But you have to consider the following problems:
- Each search engine works in a different way, so you will probably need to change most of the code to adapt it to each search engine (and you'd end up with an ugly program).
- Search engines are always under development. I can think of a monthly changing base. This would mean that almost every 30 days you would need to update your scripts or they will stop working.

It won't be VERY hard, but you need a really good reason to do that. And.. if you end up doing it, it would be nice to publish the results in a free php scripts database :-)
0
08718712060Author Commented:
I would definatly publish the code, after all that is the joy of O/Source and PHP. Could you provide a basic example as I am new to php.

Cheers in advance
0
aolXFTCommented:
Personally I wouldn't recommend it. Google are sure to not like people leaching off their search engine, and are sure to change the parsing of their html differently.

Having that said:

<?php

$html = file_get_contents("http://www.google.ie/search?q=google+php&ie=UTF-8&oe=UTF-8&hl=en&meta=");

$matches = array();

// Google stores the results in a <div>
preg_match("#<div>(.*?)</div>#is", $src, $matches);
$results = $matches[1];

// Google starts each result with a <p class=g> tag // Now I know that google isn't xhtml compliant.
...
use regular expressions to sort the rest out for you.
...

?>

Another approach would involve making googles output xhtml compliant, using something like tidy, and then using something like domxml, etc.

It would look like it would be simpler to do something like this if you had something like DOM to fall back on, like you do on the clientside.
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Giovanni GCTOCommented:
nice function that file_get_contents(), i didn't notice it. I thought there was only file() doing something like that. Nice to know.
0
08718712060Author Commented:
aolXFT,
Great stuff, as a novice I am struggling with what is probably a simple task but I am easger to do this.

Your code will return the results matching the submitted keywords but how can I then increment through the $matches to find my URL to show a listing number.

What I am trying to achive is a single php script with my own keywords in it to show something like

"Today I am ranked at number XYZ using keywords (X,Y,Z)"

Please can you help me further?.
0
aolXFTCommented:
Hmmm

I think I understand you better now, you only want to search for a few words, and see where on that list your entry is, okay, the following is some VERY UGLY CODE, but it might do the trick:


################################
<?php

$words = "X Y Z";
$my_url = "http://www.example.com";

$html = file_get_contents('http://www.google.com/search?num=100&q=' . urlencode($words));

if(strpos($html, $my_url) !== false){
    list($above, $below) = explode($my_url, $html, 2);
    $pos = substr_count($above, "<p class=g>");
} else {
    $pos = 0;
}

$str = $pos ? "We are position: $pos" : "We are not in that list";

?>
#########################################
I'm being kicked out of the computer suite now, so I haven't time to test this.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
08718712060Author Commented:
aolXFT
Absaloutly spot on!. Thank you for your quick response and a big thank you to ThG who also dived on this query.

I will continue to flick through my copy of "Beginning PHP4" and see how far I get with my project. Like ThG suggested if all works out well, the code will be released in free forums around the net (With obvious mention of you both in the headers!).

Thanks to you both!
S
0
Giovanni GCTOCommented:
Hi 08718712060,
If you set up yourself seriously in this project, keep us informed. If you just want to show us some code open a question with a small amount of points and make clear in the subject that it's related to this thread.

The problem is that a script like this, even being useful, would stop working very soon for the reasons I explained above.

Good luck
0
08718712060Author Commented:
Hi ThG,
I agree completely with what you are saying so I think I need an extra validation in there so that everytime it runs it checks to see if the start and end html tags are the same, if not it should email myself or the administrator to investigate and suspend that particular search engine. Idealy it would be great to put some sort of logic in there that looks for a pattern and re-ammends itself. This could work for the results but it wouldnt work on the search string itself.

So far so good though. I code a great deal in Delphi but php is completely new to me so I am having fun not having to worry about screen designs for a change! lol

Thanks for your help and I will try and push it on and keep you informed etc.

Cheers

S
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.