How To Programatically Find the Social Statistics of Your Website using PHP

Whether you’re in the SEO business or just have a general curiosity about your website’s popularity, it’s nice to see the numbers on your share/+1/tweet buttons climb. But what if you wanted to check the values of those numbers programatically? With most of the sites offering RESTApi methods, you can get the numbers rather simply with an http GET request.


Call to the below listed urls, inserting the URL of the site and you’ll recieve a json object with the desired information.

We have a couple of choices when it comes to how to get the json object from the url. We could use the file_get_contents method, but, while simple, this method is more prone to errors. So instead, let’s use the cURL method.

	$ch = curl_init();
	$options = array(  			//Create an array of the curl options.  Useful when you're checking multiple sites.
		CURLOPT_URL => $url, 		//Above URL appended to your URL.
		CURLOPT_HEADER => 0,		//We don't need the header of the object.
		CURLOPT_RETURNTRANSFER => 1); 	//If this option were set to 0 the data would print to the browser
	curl_setopt_array($ch, $options);
	$output = curl_exec($ch);		//Executes the cURL and places returned json into the $output variable.
	curl_close($ch);

Google, on the other hand, requires a POST request. This is not, however, impossible using cURL. The below code creates the POST request with the necessary options required to get the correct json object back.

	$url = [YOUR URL HERE];

	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
	curl_setopt($curl, CURLOPT_POST, 1);
	curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . 	'","source":"widget","userId":"@viewer","groupId":"@sel    f"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
	$output = curl_exec ($curl);
	curl_close ($curl);

Now we have a json object for each website. Time to decode and find the counts. We’ll use the json_decode method to create an associative array of each json object:

array = json_decode($output, true); 	//Decodes the json object into an associative array 
  • Facebook:
    $shares = $array['shares'];
    
  • Twitter:
    $tweets = $array['count'];
    
  • Reddit:
    $reddit_ups = $array['data']['children']['data']['ups']; 	//If you really want to see how many people disliked your site, just change ups to downs.
    
  • Stumbleupon:
    $stumbles = $array['result']['views'];
    
  • Google:
    $plusones = $array['result']['metadata']['globalCounts']['count'];
    

And there you have it. All of the social statistics for your website from the most common social sharing sites. Thanks to Tom Anthony for his help with Google+.

Trevor Boland
About Trevor Boland
Trevor is a staff writer for the iEntry Network.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>