PHP Script to get the PageRank of pages linking to you
Welcome Stumblers! There’s plenty more cool php trickery coming up in the next week or so, so don’t forget to subscribe to our feed, so you don’t miss a thing!
Darren the Edmonton SEO asked in our Yammer Network today how I got hold of the pagerank of all the sites in my post listing blogs with the top commentators plugin.
I’ve actually seen the question of how to get a page’s pagerank with PHP asked quite a bit online, so I thought I’d answer it in a post.
First things first, you need this script from here.
Now let’s talk about how to use it. There’s no better way to learn something by example, so today we’re going to knock up a script that gets the top 100 results from Yahoo! Site Explorer of pages linking to your site, and the pagerank of each of them. (sounds like fun, hey!)
Here’s the script (it’s well commented to explain how it works, you can download it here):
<?php
ob_start();
//Put the URL you want to check here
$you = “http://pimpmypagerank.com”;
//start our count at zero, so we can number the list
$n = 0;
//here we are including the pagerank.php script you downloaded (make sure that pagerank.php is in the same directory as this script)
include(’pagerank.php’);
//now let’s make a URL to get our list of backlinks from Yahoo!
$url = “http://siteexplorer.search.yahoo.com/search?p=” .urlencode($you). “&bwm=i&bwmo=d&bwmf=u&b=”;
//go get the above page from Yahoo!
$content = file_get_contents($url);
//do a bit of sexy regular expression work to pull out the links from the yahoo page
$root= (’/<span class=\”url\”>(.+?)<\/span>/’);
preg_match_all($root,$content,$base);
//loop through all the URL’s of our backlinks one by one
foreach ($base[1] as $link) {
$link = str_replace(”<br />”, “”, $link);
//add one to our counter
$n = $n + 1;
//get the pagerank of the link in question
$pr = getPageRank($link);
//the pagerank script returns the value of (-1) if the page has no pagerank. This looks crap, so we’re going to change that to Zero
if ($pr < 0) {
$pr = 0;
}
//print out our link, along with its pagerank, to our page.
echo $n. ‘ - <a href=”‘ .$link. ‘”>’ .$link. ‘</a> - pr<font color=”green”><strong> ‘ .$pr. ‘</strong></font><br>’;
ob_flush();
flush();
}
?>
You can see this script in action here, listing the top 100 backlinks to pimpmypagerank.com
