Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

PHP DZone Story Information Grabber

DZone's Guide to

PHP DZone Story Information Grabber

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

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">(.*)/isU',$dzone_content);  
$votes_down = get_match('/class="ldStats-down">(.*)/isU',$dzone_content);  
$views = get_match('/<li class="ldStats-views">(.*)/isU',$dzone_content); 
$clicks = get_match('/<li class="ldStats-clicks">(.*)/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];  
}

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.

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}