Check Google PageRank with Perl

Last updated: Aug 28, 2008

I am on a huge Perl kick lately. I have been making tons of different perl scripts for many different uses. Some of them useful and some are just plain silly. In this post we are going to talk about making an easy script that will go out and check the pagerank of any site that you want. Then we will make the script run through PHP so we can display our result on a website.

The Perl

This is a really simple perl script. Basically all we are doing here is calling a third party perl module called, “WWW::Google::PageRank”. Perl like many language is known for having a repository of easy to use modules that are already made so you don’t have to do any extra coding. This repository is called CPAN. No heavy coding in this example.

#!/usr/bin/perl
# Library location for custom modules on dreamhost
use lib '/home/marcrosoft/perlmods/share/perl/5.8.4/';
use WWW::Google::PageRank;

$url = $ARGV[0];

if (defined($url)) {
        my $pr = WWW::Google::PageRank->new;
        print scalar($pr->get($url)), "\n";
} else {
        print 'Page not specified!'. "\n";
}

The first couple of lines initiate perl and get library files. If you are using Dreamhost as your webhost you can follow this guide to download perl library modules into your home directory.

The ‘$ARGV[0];’ part of the code simply means to accept a paramter when executing the script. So you can do stuff like, ‘perl myperl.pl someArgument’.

The rest of the code is just initilizing the module or printing an error message if no argument is passed.

To execute your code do, perl whateverYouNamedYourScript.pl http://www.yoursite.com’.

The PHP Code

Once you have your working perl file you should be able to execute it from your shell. If all goes well it should return an integer between 1 and 10 representing the site’s google page rank. Now if you want to make PHP display the rank you can use the ‘shell_exec’ command to execute and return the output.

<?php
$output = shall_exac("perl /home/username/Pagerank.pl http://www.yourpage.com");
echo "The page rank for this site is: $output";
?>

Note: You must change, shall_exac to shell_exec. Due to the security settings on this domain I can’t even post the correct command anywhere near php code without getting an error in wordpress. Kinda weird but I am not complaining.

If you noticed in the above code you usually have to specify the absolute path to the perl file. Some sites are also setup to only run executable files from outside of the web path for security reasons. This is why I stored my file in my home directory.

Thats it!

Need to print shipping labels on your site?

Checkout my product RocketShipIt for simple easy-to-use developer tools for UPS™ FedEx™ USPS™ and more.

Get notified on new posts or other things I'm working on

Share: