Login
Code PHP untuk mendapatkan jumlah share count social media

Code PHP untuk mendapatkan jumlah share count social media

Saya pernah membuat jQuery plugin social media share button dan tools untuk memeriksa popularitas suatu situs di social media. Yang menunjukkan jumlah total share Google+, Twitter, Facebook, Stumbleupon, Reddit, Linkedin dan Pinterest untuk website atau URL.

Berikut adalah class php untuk mendapatkan jumlah share si sosial media. Hal ini mungkin dapat bermanfaat untuk sobat

Class

<?php

   class share_count {

     private $url,$timeout;

     function __construct($url,$timeout=100) {

       $this->url=rawurlencode($url);

       $this->timeout=$timeout;

     }

     function get_plusones()  {

       $curl = curl_init();

       curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");

       curl_setopt($curl, CURLOPT_POST, true);

       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

       curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'.rawurldecode($this->url).'","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));        $curl_results = curl_exec ($curl);        curl_close ($curl);        $json = json_decode($curl_results, true);        return isset($json[0]['result']['metadata']['globalCounts']['count'])?intval( $json[0]['result']['metadata']['globalCounts']['count'] ):0;
     }      function get_upon()  {        $json = file_get_contents("http://www.stumbleupon.com/services/1.01/badge.getinfo?url=".$this->url);        $result = json_decode($json);        if(!empty($result->result->views)){          $upon = $result->result->views;        }else {          $upon = 0;        }        return $upon;      }      function get_facebook()  {         
       $json = file_get_contents('https://api.facebook.com/method/links.getStats?urls='.$this->url.'&format=json');
       $result = json_decode($json);
       return $result[0]->share_count;
     }     
     function get_linkedin()  {           
       $json = file_get_contents('http://www.linkedin.com/countserv/count/share?url='.$this->url.'&format=json');
       $result = json_decode($json);
       return $result->count;
     }     
     function get_pinterest()  {           
       $data = file_get_contents('https://api.pinterest.com/v1/urls/count.json?url='.$this->url);
       $json = str_replace('receiveCount(', '', $data);
       $json = str_replace('})', '}', $json);
       $result = json_decode($json);
       return $result->count;
     }     
     function get_bufferapp()  {           
       $json = file_get_contents('https://api.bufferapp.com/1/links/shares.json?url='.$this->url);
       $result = json_decode($json);
       return $result->shares;
     }     
     function get_reddit()  {           
       $json = file_get_contents('http://www.reddit.com/api/info.json?url='.$this->url);
       $result = json_decode($json);
       return count($result->data->children);
     }     
   }   
?>

Contoh memanggil classnya

<?php   
   $url = 'http://twitter.com';
   $obj = new share_count($url);
   $arr = array('google_plus' => $obj->get_plusones(),
           'stumbleupon' => $obj->get_upon(),
           'facebook' => $obj->get_facebook(),
           'linkedin' => $obj->get_linkedin(),
           'pinterest' => $obj->get_pinterest(),
           'bufferapp' => $obj->get_bufferapp(),
           'reddit' => $obj->get_reddit(),
           'twitter' => 0 //Twitter kills share counts API http://cdn.api.twitter.com/1/urls/count.json?url=
         );
   header('Content-Type: application/json');
   echo json_encode($arr, JSON_PRETTY_PRINT);
?>

Nanti outputnya dalam format JSON

Biar bisa digunakan menggunakan jQuery. hhe

{
  "google_plus": 68474,
  "stumbleupon": 26763,
  "facebook": 333609,
  "linkedin": 335213,
  "pinterest": 672,
  "bufferapp": 38966,
  "reddit": 25,
  "twitter": 977738
}