Bright Hub
 
Matthew Casperson's Hubfolio

Create an inbound link count widget with PHP and Yahoo

Article by Matthew Casperson (4,911 pts )
Published on Nov 10, 2009

This free tutorial shows you how to count the number of incoming links to a page using PHP and Yahoo Site Explorer.

Introduction

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.

Yahoo Site Explorer and PHP screenshot

Step 1 - Obtain a Yahoo AppKey

Using the Yahoo Site Explorer API requires an application key, which you can obtain for free here.

Step 2 - Build up the REST URL

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);

Step 3 - Call the service

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);

Step 4 - Convert the results

The JSON results are then converted to a PHP object using the json_decode function.

$results = json_decode($json);

Step 5 - Display the results

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'};

Yahoo Site Explorer and PHP screenshot

Conclusion

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

Search More About:

 
Follow Matthew Casperson
Receive weekly updates from Matthew Casperson
 
Bright Hub - Science & Technology Articles, Buyer's Guides, How-To Tips and Software Reviews
About Bright Hub | Contact Us | Advertise with Us | Become a Writer | RSS | Site Map | Terms of Use | Privacy Policy | Copyright Policy
©2010 Bright Hub Inc. All rights reserved. Page copy protected against web site content infringement by Copyscape