Brian Cray ··· Home > Blog > Php, Social media, Analytics > Display popular posts based on social media engagement with PostRank Top Posts API

Display popular posts based on social media engagement with PostRank Top Posts API

According to PostRank, "Over 80% of the engagement with your content doesn't happen on your blog." That's a big deal on many levels. In the context of this post, it confirms that number of comments alone doesn't constitute the popularity of your blog posts.

Lucky for us though, PostRank—a company that monitors and collects social engagement data in real-time across the web—provides powerful Data Services that give developers API access to that social engagement data.

Today I'll show you how to use PHP5 to fetch and display your blog's most popular posts using PostRank's Top Posts API.


  1. Your blog must publish an RSS feed
  2. Grab your PostRank feed hash (directions
  3. PHP5 is required.

Fetching the Top Posts API

This code caches the latest Top Posts API feed for an hour to maintain your website's performance.

function popular_postrank()
    //BASIC REST API URL:{feed_hash}/topposts?
    //replace this with YOUR feed hash as discussed in the requirements
    $postrank_feed_hash = '3e7883668d6a7406078846eed577d4f8';

    $numposts = 10;
    $apikey = $_SERVER['HTTP_HOST'];
    $url = '' . $postrank_feed_hash . '/topposts?appkey=' . $apikey . '&format=json&num=' . $numposts;
    $cache = dirname(__FILE__) . '/caches/postrank';
    if(filemtime($cache) < (time() - 3600))
        mkdir(dirname(__FILE__) . '/caches', 0777);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
        $data = curl_exec($ch);
        $cachefile = fopen($cache, 'wb');
        fwrite($cachefile, $data);
        $data = file_get_contents($cache);
    $json = json_decode($data);
    $html = '<ul>';
    foreach($json->items as $item)
        $html .= '<li><a href="' . $item->original_link . '">' . $item->title . '</a></li>';
    $html .= '</ul>';
    echo $html;

Displaying the popular posts

Put this anywhere you want to display your top posts.

<?php popular_postrank(); ?>

HTML output

Calling the above function outputs a simple HTML unordered list with the CSS class name "popular-posts", like so:

<ul class="popular-posts">
    <li><a href="">Diving deep into user behavior with Google Analytics, Event Tracking, and jQuery</a></li>
    <li><a href="">Free PHP URL shortener script that kicks ass</a></li>
    <li><a href="">How wireframing makes your website designs better</a></li>
    <li><a href="">Online community best practices: Effective signup process</a></li>
    <li><a href="">Fire the “web designer”</a></li>
    <li><a href="">How to track inbound links from your social media profiles (Twitter, Facebook, etc)</a></li>
    <li><a href="">Free PHP URL shortener script that kicks ass</a></li>
    <li><a href="">Wireframing Freebie: 960 grid template for OmniGraffle</a></li>
    <li><a href="">Smart web form validation with PHP</a></li>
    <li><a href="">Impossibly simple image randomizer with jQuery</a></li>

Adding it to Wordpress

  1. Add the popular_postrank() function to /wp-content/themes/{current_theme}/functions.php
  2. Add the call to the function anywhere you want to display to top posts (likely index.php, archives.php, or single.php)