Posts Tagged ‘forums’
Whatpulse API
I was reading this thread: http://forums.whatpulse.org/showthread.php?p=22448#post22448 and thought I had the solution here:
<?php
//** read whatpulse userstats **//
// $userid = your whatpulse userid
// $optionsArr = the items you want (read the xml file first)
function readUserStats($userid, $optionsArr) {
// prepare an array to hold your stats
$WhatPulseStats = array();
// types of statistics
$stat_types = $optionsArr;
if(count($stat_types) == 0) {
$stat_types = array(”AccountName”,
“TotalKeyCount”,
“TotalMouseClicks”,
“Rank”,
“GeneratedTime”,
“TotalMiles”);
}
// init the xml parser and read the data into an array
$data = file(”http://whatpulse.org/api/user.php?UserID=”.$userid);
$data = implode(”", $data);
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $values, $tags);
xml_parser_free($parser);
// loop through the structures
foreach ($tags as $key => $val) {
// only process stuff between the tags
if ($key == “UserStats”) {
// loop through the tags inside
$ranges = $val;
for ($i = 0; $i < count($ranges); $i += 2) {
$offset = $ranges[$i] + 1;
$len = $ranges[$i + 1] – $offset;
$statsarray = array_slice($values, $offset, $len);
// loop through the structure of the xml tag
foreach($statsarray as $key => $value) {
// match to a stats_type
for($i = 0; $i < count($stat_types); $i++) {
if($value['tag'] == $stat_types[$i]) {
// remember the value of the stats_type
$type = $stat_types[$i];
$WhatPulseStats[$type] = $value['value'];
}
}
}
}
}
else {
continue;
}
}
return $WhatPulseStats;
}
?>
send me a comment if it works