Hacking Social Networks

Share This:

If you like my work show support on my Patreon
Recently I had a task that required me to get the amount of times a website was shared on each social network, and then add the results of all the social shares together. Another requirement was that I couldn’t use any of the social network API.

After thinking about how to go about doing this I eventually decided to utilize JSON data. Sites like twitter and facebook have API’s that use JSON files. And it may be true that your limitted to what you can do with the JSON data without the API’s you can still get some decent results.

For example if we check out the facebook JSON data for Google we would see the following.

   "id": "http://www,google.com",
   "shares": 4087497,
   "comments": 2158

This is pretty self explanitory. We need to extract the number next to “shares” : The same is true if we do this with twitter which displays the following data.


In the twitter one we need to get the number next to “count”:

These where pretty simple to do using PHP. We create a custom function.

A custom PHP function for twitter looks similar to the following.

function myTwitterCounter($url) {
$s = file_get_contents("http://urls.api.twitter.com/1/urls/count.json".
return isset($ar[2]) ? $ar[2] : 0;

A custom function for Facebook looks like the following.

function myFBCount($urlid) {
if(is_int($urlid)) $url = 'http://graph.facebook.com/' . $urlid;
else $url = 'http://graph.facebook.com/' .  $urlid;
$json = json_decode( file_get_contents($url), false );
if(isset( $json->likes )) return (int) $json->likes;
elseif(isset( $json->shares ) ) return (int) $json->shares;
return 0;

Now we can just call them by simply echoing them both.

echo myTwitterCounter('http://www.mysite.com');
echo myFBCount('http://www.mysite.com');

That was easy enough for those two. But Google + was a bit more difficult. But a solution for this eventually came to me as well. And it came to me with the Google +1 button. And even though it wasn’t as smooth and elegant as Facebook or Twitter it gets the job done. If we go to a URL like the following.

https://plusone.google.com/_/+1/fastbutton?url=http://www.facebook.com we see the Google +1 button only with the amount of pluses for Facebook. So after some trial and error I finally worked out the following code.

function gplus($url) {
$contents = file_get_contents( 'https://plusone.google.com/_/+1/fastbutton?url=' .  $url );
preg_match( '/window\.__SSR = {c: ([\d]+)/', $contents, $matches );
if( isset( $matches[0] ) )
return (int) str_replace( 'window.__SSR = {c: ', '', $matches[0] );
return 0;

Once again the only thing we need to do now is echo the function to get the number.

echo gplus('http://www.mysite.com');

Now lets add our numbers up.

$google = gplus('http://www.mysite.com');
$facebook = myFBCount('http://www.mysite.com');
$twitter = myTwitterCounter('http://www.mysite.com');
$totalshares = $google + $facebook + $twitter;
echo $totalshares;

Pretty simple isn’t it. Now you have a custom dynamic social sharing widget for your website.
If you like my work show support on my Patreon