PHP DZone Story Information Grabber

By  on  

Having a user submit a post to DZone is great for several reasons:

  • A reader thinks enough of what you wrote to want to share it with others.
  • Your blog gets more readership.
  • Your blog revenue increases.
  • The inlink and traffic boost improve your search engine rank and Alexa ranks.
  • The new visitors may post comments, which may enhance your article.

Surely there are numerous more. Thus, it's important to keep track of the information about your article provided by DZone. Luckily, using some quick PHP code, you can grab relevant information right from DZone. All you need to know is the DZone URL that features your article.

The Code

/* step 1:  download the page content */
$dzone_content = file_get_contents('http://www.dzone.com/links/f_programming_fast_guide.html');

/* step 2:  parse it! */
$votes_up = get_match('/class="ldStats-up">(.*)<\/li>/isU',$dzone_content);
$votes_down = get_match('/class="ldStats-down">(.*)<\/li>/isU',$dzone_content);
$views = get_match('/class="ldStats-views">(.*)<\/li>/isU',$dzone_content);
$clicks = get_match('/class="ldStats-clicks">(.*)<\/li>/isU', $dzone_content);

/* step 3:  echo */
echo 'Up Votes: ',$votes_up,'<br />';
echo 'Down Votes: ',$votes_down,'<br />';
echo 'Views: ',$views,'<br />';
echo 'Clicks: ',$clicks,'<br />';

/* helper:  does regex */
function get_match($regex,$content)
{
	preg_match($regex,$content,$matches);
	return $matches[1];
}

One note: each usage of this script increments DZone's "views" stat, so if you plan on keeping accurate statistics, record the number of requests to the page and subtract that from the total.

Recent Features

  • By
    Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...

  • By
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

Incredible Demos

  • By
    FileReader API

    As broadband speed continues to get faster, the web continues to be more media-centric.  Sometimes that can be good (Netflix, other streaming services), sometimes that can be bad (wanting to read a news article but it has an accompanying useless video with it).  And every social service does...

  • By
    HTML5 download Attribute

    I tend to get caught up on the JavaScript side of the HTML5 revolution, and can you blame me?  HTML5 gives us awesome "big" stuff like WebSockets, Web Workers, History, Storage and little helpers like the Element classList collection.  There are, however, smaller features in...

Discussion

  1. I added some lines for those who are behind a proxy, also you have an extra on line 8 but this is a good one to track your posts on DZone :)

    /* step 1: download the page content */
    $default_opts = array(‘http’=>array(
    ‘method’=>”GET”,
    ‘header’=>”Accept-language: en\r\n” . “Cookie: foo=bar”,
    ‘proxy’=>”tcp://http-proxy.mycompany.com:80″));
    $def = stream_context_get_default($default_opts);
    $dzone_content = file_get_contents(‘http://www.dzone.com/links/f_programming_fast_guide.html’);
    
    /* step 2: parse it! */
    $votes_up = get_match(‘/class=”ldStats-up”>(.*)/isU’, $dzone_content);
    $votes_down = get_match(‘/class=”ldStats-down”>(.*)/isU’, $dzone_content);
    $views = get_match(‘/(.*)/isU’, $dzone_content);
    $clicks = get_match(‘/(.*)/isU’, $dzone_content);
    
    /* step 3: echo */
    echo ‘Up Votes: ‘,$votes_up,”;
    echo ‘Down Votes: ‘,$votes_down,”;
    echo ‘Views: ‘,$views,”;
    echo ‘Clicks: ‘,$clicks,”;
    
    /* helper: does regex */
    function get_match($regex, $content){
    preg_match($regex, $content, $matches);
    return $matches[1];
    }
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!