This free tutorial shows you how to count the number of incoming links to a page using PHP and Yahoo Site Explorer.
DOWNLOAD THE SOURCE CODE
Everyone loves a counter of some kind. A few years ago page hit counters were all the rage. Now social media links like Twitter and Facebook posts and RSS feed readers are proudly counted and displayed.
But the best indication of the popularity of a page is still the humble inbound link. It forms the basis of most search engine rankings, and is a good indicator of how much traffic a page will recieve.
This tutorial will show you how to count the number of incoming linkes with PHP and Yahoo Site Explorer.
Using the Yahoo Site Explorer API requires an application key, which you can obtain for free here.
The Yahoo Site Explorer REST URL that will be called is built up using the base url, the appkey, the url whose inbounds links we want to counts and the format of the result. Here we build up the final url using these base components.
$appKey = 'YourYahooAPIKey';
$yseUrl = 'http://search.yahooapis.com/SiteExplorerService/V1/inlinkData';
$query = 'http://www.brighthub.com/hubfolio/matthew-casperson/blog/archive/2009/08/23/tutorial-index.aspx';
$queryUrl =
$yseUrl .
"?appid=" . $appKey .
"&output=json" .
"&query=" . rawurlencode($query);
Here we use the Curl library to query the Yahoo Site Explorer REST API.
$session = curl_init($queryUrl);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($session);
curl_close($session);
The JSON results are then converted to a PHP object using the json_decode function.
$results = json_decode($json);
Yahoo Site Explorer returns a number of details about inbound links. For this counter we only want the totalResultsAvailable property. But as the screenshot below (of the excellent Online JSON Parser) shows, you can find out much more about the links than this simple count.
print $results->{'ResultSet'}->{'totalResultsAvailable'};
This code can also be used for your own internal SEO analysis, by allowing you to see which pages have received the most inbound links. You could also extend the code to identify those domains that are linking to your pages, allowing you to more effectively target you advertising and partnerships.
Return to the Tutorial Index