
03-Sep-2003, 17:11
|
 |
Regular Member
|
|
Join Date: Jan 2003
Location: Loughborough, England
Posts: 840
|
|
|
Check your keyword position using Google API
I received this via the JimWorld newsletter that I'd apparently signed up to, but since it is under the GPL I thought I'd post it here:
Quote:
Pull up a seat, boys and girls... this one's worth the price of admission.
We're going to work this week on a cool tool that uses the GoogleAPI. My intent
here is to show you just how easy it is to use, and how you can do something
cool, if you just think outside the box a bit. This code sample was done in
PHP, but can be easily replicated in Perl, or any other language that has simple
opject access protocol (SOAP) support. The relevant libraries that you need
are listed at the end of this article. Note that in most cases, your ISP's
installation of Perl or PHP will not include the SOAP extensions by default, so
you'll have to ask that they be installed for you. If you run your own machine,
you should be able to install the necessary core libraries by yourself in just a
few minutes. The "SOAP_Google" class listed below, which is the basis for this
article, is free for your use by visiting the author's Web site. You can
install it into the same directory as your GoogleAPI scripts. Ok, here we
go...
One of the things essential to any SEO, and really, to anyone who operates a Web
site, is to know where they rank at Google for a given search term or phrase.
Using the GoogleAPI, we can very easily find out where a site ranks, without
having to visit Google.com and going dizzy reading the results. The source code
for "test.php" follows, and is commented so that you can follow the logic.
Happy position checking!
|
PHP Code:
<?php
// test.php
// A simple way to check positions for a given domain, for given
//keywords at Google.com. Uses the googleAPI to make things easier
// This application is free for non-commercial use under the GPL
// Please keep this copyright and header intact.
// (c) 2003 John Cokos, JimWorld.com
// This pulls in the SOAP_Google Class file
//require_once 'google.php';
// For Clarity, let's use real variable names //
$terms = $_POST[keyword];
$domain = $_POST[domain];
// Draw a very simple form //
echo "
<table>
<tr><td>
<form action='test.php' method='post'>
Keyword / Phrase: <input name='keyword' size='50' value='$terms' />
<br />
Domain to Check: <input name='domain' size='50' value='$domain' />
<br />
<center><input type='submit' /></center>
</form>
</td></tr>
</table>
<hr />
";
// If the form has been filled out .... get to work.
if ( $terms && $domain ) {
// Create a new oject using the SOAP_Google Class. All you need to
// Do is give it your googleAPI license key
$google = new SOAP_Google('YOUR-GOOGLE-LICENSE-KEY');
// Use that Google object to run a search. In this example, we simply
// pass along the search terms, and ask for the first 10 results. You
// can also specify a higher number of results, where to start from,
// turn on safe searching, etc. For our purposes, we just want the
// basic top 10 results.
$result = $google->search(
array(
'query' => $term,
'maxResults' => 10
)
);
// Now, the results of searching Google are in an object called
"$result"
// This is actually an array of objects, that we can iterate through
// to do what we want
// Make sure that we actually got results //
if (false !== $result) {
// Tell the user what we have done //
echo "Checked: <b>$domain</b> for <b>$term</b> .... <br /><br />";
// What we're going to be doing is reporting the position of our
//domain
// For the given search term, so first, we make sure that we're
//starting
// at "0", so that the counter works right.
$position = 0;
// Now, Iterate through the result set. Each "$listing" is a PHP
//Object
// that contains the data for the current search result in the list
foreach ( $result->resultElements as $listing ) {
// Increase the position Counter
$position++;
// If the domain we asked to check ($domain) is contained
//within the URL of the current search result, we have a match,
//so let's display it on screen just like a Google result. Note
//that for the purposes of what we're doing, we really don't
//need to display the listing, just report the position, but I
//wanted you to see how to manipulate the results.
// Note that this will show you every match of the domain that
//it finds so that you can see if you appear multiple times.
if ( eregi( $domain, $url ) ) {
// Create some logical variable names //
$title = $listing->title;
$snippet = $listing->snippet;
$description = $listing->summary;
$url = $listing->URL;
// Dump it out just like a listing //
echo "
<div style='font-family:verdana,arial; font-size:.8em'>
<blockquote>
<b>Position: $position</b><br /><br />
<a href='$url'>$title</a><br />
$snippet</br />
<span style='color:#708090'>Description:</span>
$description<br />
<span style='color:green'><i>$url</i></span>
</div>
</blockquote>
<hr />
";
$found = 1;
}
}
// If we did not appear in the top 10, report it.
if ( ! $found ) { echo "Not found in first 10 positions"; }
}
// This will return a simple error message if we got nothing back
//from Google
else {
echo 'Query failed.';
}
}
// This will show if the form was not filled out //
else { echo "Please fill in the form above completely"; }
?>
Resources:
Google API Sign up: http://www.google.com/apis/
PHP PEAR::SOAP Extension: http://pear.php.net/package-info.php?pacid=87
SOAP_Google Class for PHP: http://www.sebastian-bergmann.de/?page=google
Perl SOAP Library: http://search.cpan.org/author/KULCHENKO/SOAP-Lite-0.55/
Rob
|