Vork is an open-source PHP framework designed for rapid development of performance-oriented scalable applications.
The mission of Vork is to provide an MVC architecture and full-featured toolkit in a gimmick-free no-frills approach without adding overhead, creating slow & unscalable abstraction layers or re-inventing native PHP functionality.
Since we can see Digg turning more into a funny-pic-and-vid site each day, I’ve turned my attention to Reddit. Reddit just seems more controlled and programmer-friendly. Since I have respect for Reddit I thought I’d put together a quick tutorial on how you can retrieve a URL’s Reddit score using PHP.
<?php /* settings */ $url = 'http://davidwalsh.name/9-signs-not-to-hire-that-web-guy'; $reddit_url = 'http://www.reddit.com/api/info.{format}?url='.$url; $format = 'json'; //use XML if you'd like...JSON FTW! $score = $ups = $downs = 0; //initialize /* action */ $content = get_url(str_replace('{format}',$format,$reddit_url)); //again, can be xml or json if($content) { if($format == 'json') { $json = json_decode($content,true); foreach($json['data']['children'] as $child) { // we want all children for this example $ups+= (int) $child['data']['ups']; $downs+= (int) $child['data']['downs']; //$score+= (int) $child['data']['score']; //if you just want to grab the score directly } $score = $ups - $downs; } } /* output */ echo "Ups: $ups<br />"; //21 echo "Downs: $downs<br />"; //8 echo "Score: $score<br />"; //13 /* utility function: go get it! */ function get_url($url) { $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1); $content = curl_exec($ch); curl_close($ch); return $content; } ?>
Parsing the JSON is simple using json_encode with the value of true to make turn the JSON into an associate array. My example shows how you can grab the number of “ups” and “downs” — not just the score. As with every API/web service, I highly recommend caching the result of your request.
Don't forget to follow me on Twitter and be sure to visit Script & Style for the best Javascript and CSS articles around! Sponsor the David Walsh Blog and get your brand in front of several thousand users per day!Get a URL’s Reddit Score Using PHP and JSON
Related posts:
Earlier in the week I published a popular article titled Dynamically Create Charts Using MooTools MilkChart and Google Analytics. My post showed you how to use MooTools MilkChart and a splash of PHP to create beautiful charts of Google Analytics data. I was interested in seeing what jQuery had to offer in the charting department. jQuery Flot is what I found.
/* defaults */ $month = date('n'); $year = date('Y'); /* submission? */ if($_GET['month'] || $_GET['year']): /* cleanse lookups */ $month = (int) $_GET['month']; if(!$month) { $month = 1; } $year = (int) $_GET['year']; if(!$year) { $year = date('Y'); } /* retrieve information from google analytics */ require 'ga/analytics.class.php'; $analytics = new analytics('youraccount@gmail.com', 'password'); $analytics->setProfileByName('yourdomain.com'); $analytics->setMonth($month,$year); $visits = $analytics->getVisitors(); $views = $analytics->getPageviews(); /* build tables */ if(count($visits)) { foreach($visits as $day=>$visit) { $flot_datas_visits[] = '['.$day.','.$visit.']'; $flot_datas_views[] = '['.$day.','.$views[$day].']'; } $flot_data_visits = '['.implode(',',$flot_datas_visits).']'; $flot_data_views = '['.implode(',',$flot_datas_views).']'; } endif;
The above code is the same as my MooTools post with the exception of the statistics output format. jQuery flot prefers arrays instead of a HTML table.
$(document).ready(function() { var visits = <?php echo $flot_data_visits; ?>; var views = <?php echo $flot_data_views; ?>; $('#placeholder').css({ height: '400px', width: '600px' }); $.plot($('#placeholder'),[ { label: 'Visits', data: visits }, { label: 'Pageviews', data: views } ],{ lines: { show: true }, points: { show: true }, grid: { backgroundColor: '#fffaff' } }); });
The above is a simple example of using jQuery Flot’s plot method. Simply provide the placeholder and statistical data from the PHP above.
What do you think? Which method do you prefer?
Don't forget to follow me on Twitter and be sure to visit Script & Style for the best Javascript and CSS articles around! Sponsor the David Walsh Blog and get your brand in front of several thousand users per day!Dynamically Create Charts Using jQuery Flot and Google Analytics
The prospect of creating graphics charts with javascript is exciting. It’s also the perfect use of javascript — creating non-essential features with unobtrusive scripting. I’ve created a mix of PHP (the Analytics class), HTML, and MooTools javascript that will connect to Google Analytics, create an HTML table with the statistics for a given month, and use MooTools MilkChart to colorfully chart them out.
/* defaults */ $month = date('n'); $year = date('Y'); $type = 'Column'; /* submission? */ if($_GET['month'] || $_GET['year']): /* cleanse lookups */ $month = (int) $_GET['month']; if(!$month) { $month = 1; } $year = (int) $_GET['year']; if(!$year) { $year = date('Y'); } /* retrieve information from google analytics */ require 'ga/analytics.class.php'; $analytics = new analytics('youraccount@gmail.com', 'yourP@ss'); $analytics->setProfileByName('yourdomain.com'); $analytics->setMonth($month,$year); $visits = $analytics->getVisitors(); $views = $analytics->getPageviews(); /* build tables */ if(count($visits)) { //visits - php $visits_table_data = '<table id="data-table-visits">'; /* $visits_table_data.= '<thead><tr><th>Unique Visits</th><th>PageViews</th></tr></head><tbody>'; */ $visits_table_data.= '<thead><tr><th>Unique Visits</th></tr></head><tbody>'; foreach($visits as $day=>$visit) { /* $visits_table_data.= '<tr><td>'.$visit.'</td><td>'.$views[$day].'</td></tr>'."\n"; $visits_table_foot.= '<td>'.$day.'</td><td>'.$day.'</td>'."\n"; */ $visits_table_data.= '<tr><td>'.$visit.'</td></tr>'."\n"; $visits_table_foot.= '<td>'.$day.'</td>'."\n"; } $visits_table_data.= '</tbody>'; $visits_table_data.= '<tfoot><tr>'.$visits_table_foot.'</tr></tfoot>'; $visits_table_data.= '</table>'; } endif;
It all kicks off with grabbing the information from Google Analytics. Simply provide the time frame you would like statistics for. I usually choose to retrieve statistics by the month.
<h2>Select Your Month/Year</h2> <form method="get"> <select name="month" id="month"> <option value="">-- Select Month --</option> <?php for($x = 1; $x <= 12; $x++): echo '<option value="',$x,'"',($month == $x ? ' selected="selected"' : ''),'>',date('F',mktime(0,0,0,$x,1,2009)),'</option>'; endfor; ?> </select> <select name="year" id="year"> <option value="">-- Select Year --</option> <?php for($x = 2008; $x <= date('Y'); $x++): echo '<option value="',$x,'"',($year == $x ? ' selected="selected"' : ''),'>',$x,'</option>'; endfor; ?> </select> <select name="type" id="type"> <option value="">-- Select Chart Type --</option> <?php $chart_types = array('Column','Bar','Line','Scatter','Pie'); foreach($chart_types as $chart_type): echo '<option value="',$chart_type,'"',($type == $chart_type ? ' selected="selected"' : ''),'>',$chart_type,'</option>'; endforeach; ?> </select> <input type="submit" name="submit" id="submit" value="Get Statistics!" /> </form> <?php //php time - echo tables if($visits_table_data) { echo '<h3>Visits</h3>', $visits_table_data,'<br />'; } ?> <?php if(count($visits)): ?> <script type="text/javascript"> var visits = new MilkChart.<?php echo $_GET['type']; ?>('data-table-visits',{ width: 960, height: 550, font: 'tahoma', showValues: false, useFooter: true }); </script> <?php endif; ?>
Using the THEAD, TBODY, and TFOOT elements is extremely important in the ensuring the correct labels are placed within the generated chart. The data table itself is very simple. MilkChart will take the above table and create a CANVAS element which will contain the chart.
There are five different chart types you may choose from: Column, Bar, Pie, Line, Scatter. MilkChart takes full advantage of MooTools’ inheritance model as each type of chart’s class extends the base MilkChart class.
I love the way the MilkChart developer(s) utilized MooTools’ OOP/inheritance model to perfection. I also love that MilkChart requires the bare minimum of data for the HTML table. MilkChart isn’t without its flaws though. A few of the table types had a fit about including multiple dimensions (using both page views and visits within the same chart, for example) and value label placement has yet to be perfected.
Don't forget to follow me on Twitter and be sure to visit Script & Style for the best Javascript and CSS articles around! Sponsor the David Walsh Blog and get your brand in front of several thousand users per day!Dynamically Create Charts Using MooTools MilkChart and Google Analytics
One way of significantly speeding up your website is by caching information you retrieve remotely from other websites or databases. Caching information is extremely simple and can save your users seconds of unnecessary waiting. Here’s how I cache my FeedBurner follower count on my site.
/* settings */ $cache_path = '/cache/'; $rss_file_name = date('Y-m-d').'-rss.txt'; /* rss */ if(file_exists($cache_path.$rss_file_name)) { $rss_subcribers = file_get_contents($cache_path.$rss_file_name); } else { $rss_content = get_url('https://feedburner.google.com/api/awareness/1.0/GetFeedData?id=dfadajf324789fhSDFDf48'); $subscribers = get_match('/circulation="(.*)"/isU',$rss_content); if($subscribers) { $subscribers = number_format($subscribers,0,'',','); file_put_contents($cache_path.$rss_file_name,$subscribers); $rss_subcribers = $subscribers; } } /* display */ echo 'My subscriber count is: ',$rss_subscribers; /* connects to the URL, returns content */ function get_url($url) { $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1); $content = curl_exec($ch); curl_close($ch); return $content; } /* helper: does the regex */ function get_match($regex,$content) { preg_match($regex,$content,$matches); return $matches[1]; }
The first thing we do is check to see if our cached file exists. If it does, we simply return its contents. If the cache file doesn’t exist, we connect to our private FeedBurner URL, parse the content, and save the feed to a file named after the current day. Note the way we can check if we had gotten the count for the current day is by naming the file in “year-month-day” format.
Don't forget to follow me on Twitter and be sure to visit Script & Style for the best Javascript and CSS articles around! Sponsor the David Walsh Blog and get your brand in front of several thousand users per day!Retrieve, Cache, and Display Your FeedBurner Subscriber Count