Current Rev: HEAD | Go To: Head

WhatPulse.class.php

Revision: HEAD - (Download) (As Text)

0. <?
1. /*
2.         Name: WhatPulse data parse class
3.        
4.         Author: John Anderton (Karun AB)
5.        
6.         Site: http://karunab.com
7.        
8.         Requirements:
9.                 1) A WhatPulse profile with webapi enabled
10.                 2) Knowledge of your UserID
11.                 3) A server with
12.                         a) php 5
13.                         b) cURL enabled
14.        
15.         Instructions:
16.                 Q1) What hacks do you use to retrieve these stats?
17.                 A1) I depend on WhatPulse's webapi to generate the stats in xml and then cURL it to a local file to read from.
18.                
19.                 Q2) Why not read the file remotely?
20.                 A2) Most of the sensible servers won't allow you to read files remotely because its not safe. cURLing is a much better option.
21.                
22.                 Q3) How do I enable webapi?
23.                 A3) Go to your WhatPulse "My Page" (http://whatpulse.org/my/) and click My Profile. Currently this page is found at http://whatpulse.org/my/profile and hopefully won't change with time.
24.                         Make sure "Generate XML statistics (webapi)" is enabled.
25.                
26.                 Q4) Where can I view my stats in xml?
27.                 A4) http://whatpulse.org/api/users/USERID_HERE.xml
28.                
29.                 Q5) How do I find out what my UserID is?
30.                 A5) Right click the WhatPulse tray icon > Account > Your Account Info... > UserID
31.                
32.         Usage:
33.                 Part 1) The server side php file to use this class (See WhatPulse_Example.php)
34.                         $wp = new WhatPulse($YourUserID,$YourCacheTime); //Cache time is optional
35.                         $stats = $wp->readUserStats();
36.                         print_r($stats);
37.                 Part 2) Calling the script
38.                         http://yoursite.com/path/whatpulse_example.php?uid=youruid
39.                
40.         Definitions:
41.                 class WhatPulse {
42.                         // Class variables
43.                         private $userID;
44.                         private $cacheFile;
45.                         private $cacheTime;
46.                        
47.                         // Basic class functions
48.                         public function readUserStats();
49.                         private function createCache();
50.                        
51.                         // Advanced class functions
52.                         public function changeCacheTime($newtime);
53.                         public function forceCacheRefresh();
54.                         public function WhatPulse($uid, $ctime=300);
55.                 }
56. */
57.
58. // Do not edit below this unless you know what you're doing.
59. class WhatPulse
60. {
61.         // Class variables
62.         // Your userid.
63.         private $userID;
64.         // Name of file you want to cache to. Automatically set to your user id by default.
65.         private $cacheFile;
66.         // Time for which cache is valid. Default = 300 seconds = 5 minutes.
67.         private $cacheTime;
68.        
69.         // Basic class functions
70.         // Read stats from the site.
71.         public function readUserStats() {
72.                 // Check if caching is required
73.                 if ($this->recacheNeeded())
74.                         $this->createCache();
75.
76.                 // WhatPulse Site code starts
77.                 // Prepare an array to hold your stats
78.                 $WhatPulseStats = array();
79.                
80.                 // Types of statistics
81.                 $stat_types = array("UserID", "AccountName", "Country",
82.                                                 "DateJoined", "Homepage", "LastPulse",
83.                                                 "Pulses", "TotalKeyCount", "TotalMouseClicks",
84.                                                 "AvKeysPerPulse", "AvClicksPerPulse",
85.                                                 "AvKPS", "AvCPS", "Rank", "TeamID",
86.                                                 "TeamName", "TeamMembers", "TeamKeys",
87.                                                 "TeamClicks", "TeamDescription",
88.                                                 "TeamDateFormed", "RankInTeam", "GeneratedTime");
89.                                                
90.              // Initialise the xml parser and read the data into an array
91.              $data = implode("", file($this->cacheFile));
92.              $parser = xml_parser_create();
93.              xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
94.              xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
95.              xml_parse_into_struct($parser, $data, $values, $tags);
96.              xml_parser_free($parser);
97.              
98.              // loop through the structures
99.              foreach ($tags as $key => $val)
100.              {
101.                 // only process stuff between the <UserStats> tags
102.          if ($key == "UserStats")
103.          {
104.                  // loop through the tags inside <UserStats>
105.                      $ranges = $val;
106.                      for ($i = 0; $i < count($ranges); $i += 2)
107.                      {
108.                                         $offset = $ranges[$i] + 1;
109.                                         $len = $ranges[$i + 1] - $offset;
110.                                         $statsarray = array_slice($values, $offset, $len);
111.                                
112.                                         // loop through the structure of the xml tag
113.                                         foreach($statsarray as $key => $value)
114.                                         {
115.                                                 // match to a stats_type
116.                                                 for($i = 0; $i < count($stat_types); $i++)
117.                                                 {
118.                                                         if($value['tag'] == $stat_types[$i])
119.                                                         {
120.                                                                 // remember the value of the stats_type
121.                                                                 $type = $stat_types[$i];
122.                                                                 $WhatPulseStats[$type] = $value['value'];
123.                                                         }
124.                                                 }
125.                                         }
126.                                 }
127.                         }
128.                         else {
129.                                 continue;
130.                         }
131.                 }
132.                
133.                 return $WhatPulseStats;
134.                 // WhatPulse Site code ends*/
135.         }
136.
137.         // If needed, this is what creates the cache
138.         private function createCache() {
139.                 $ch = curl_init("http://whatpulse.org/api/users/".$this->userID.".xml");
140.                 $fp = fopen($this->cacheFile, "w");
141.
142.                 curl_setopt($ch, CURLOPT_FILE, $fp);
143.                 curl_setopt($ch, CURLOPT_HEADER, 0);
144.
145.                 curl_exec($ch);
146.                 curl_close($ch);
147.                 fclose($fp);
148.         }
149.        
150.         // Advanced class functions
151.         public function changeCacheTime($newtime) {
152.                 $this->cacheTime = $newtime;
153.         }
154.        
155.         // Force a cache update immediately
156.         public function forceCacheRefresh() {
157.                 $this->createCache();
158.         }
159.        
160.         // Is a re-cache needed?
161.         public function recacheNeeded() {
162.                 if (!file_exists($this->cacheFile))
163.                         return true;
164.                 else if ((time() - filemtime($this->cacheFile)) > $this->cacheTime)
165.                         return true;
166.                 else
167.                         return false;
168.         }
169.        
170.         // Class constructor
171.         // Takes userID and cacheTime as input
172.         public function __construct($uid, $ctime=300) {
173.                 if ($uid != null && $uid > 0)
174.                 {
175.                         $this->userID = $uid;
176.                         $this->cacheFile = "$uid.xml";
177.                         $this->cacheTime = $ctime;
178.                 }
179.                 else if ($uid == null)
180.                         exit("No UserID provided.");
181.                 else
182.                         exit("Invalid UserID provided.");                     
183.         }
184. }
185.
186. ?>