I am trying to add a Twitter count number and a Feedburner count number but when I combine the two with basic addition, it fails! If I get the valkues and print them seperatley it works fine, just will not let me add the two…
$twitCnt = twitter_subscribers(TWITTER_USERNAME);
$feedCnt = feed_subscribers(FEEDBURNER_USERNAME);
$totalCnt = $twitCnt + $feedCnt;
echo $totalCnt;
Assume that $twitCnt is = 2000 and $feedCnt is = 1000
Now when I try to add the 2 instead of getting 3000 I will get the $feedCnt value + 1 = 1001 instead of 3000
I am currently stumped, If I print the $twitCnt and the $feedCnt they show the correct amounts, however when I add the 2 in my code, the $twitCnt is shown as = 1 instead of it’s actual value.
Any ideas what would cause this?
Update after running var_dump($twitCnt, $feedCnt)
string(5) "3,000"
object(SimpleXMLElement)#238 (1) {
[0]=>
string(5) "1,000"
}
Also the Functions for getting the stats…
function twitter_subscribers($username = 'yourname'){
$count = get_transient('twitter_count');
if ($count != false){
return $count;
}else{
$count = 0;
$url = 'http://api.twitter.com/1/users/show.xml?screen_name='. $username;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$count = $xml->followers_count;
$count = (float) $count;
$count = number_format($count);
set_transient('twitter_count', $count, 21600); // 6 hour cache
return $count;
}
}
function feed_subscribers($username = 'yourname')
{
$feed_url = 'http://feeds.feedburner.com/' . $username;
$count = get_transient('rss_count');
if ($count != false)
return $count;
$count = 0;
$data = wp_remote_get('http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' .
$feed_url . '');
if (is_wp_error($data)) {
return 'error';
} else {
$body = wp_remote_retrieve_body($data);
echo $body;
$xml = new SimpleXMLElement($body);
$status = $xml->attributes();
if ($status == 'ok') {
$count = $xml->feed->entry->attributes()->circulation;
} else {
$count = 300; // fallback number
}
}
set_transient('rss_count', $count, 21600); // 6 hour cache//60*60*24
return $count;
}
Well, first
$twitCntis a formatted string (I don’t know why you format the value directly into the variable, since you need to compute that value prior to display it, you should only format it when displaying it). And$feedCntis an object of typeSimpleXMLElement.To fix this, do not format
$twitCntbut only when you display the value (ie. echo it) to the user. And fix this line$count = $xml->feed->entry->attributes()->circulation;so it returns a numeric value instead of an object.** Update **
I took a small piece of code found here and giving it to you as a solution to convert your formatted numbers to actual numeric values.
For instance, it will convert
1,200.123into1200.123But why go complicated when you can simply do :