/**
  * Get social share count.
  * @param string $url
  * @param string $socialNetwork
  */
 private function getCount($url, $socialNetwork)
 {
     if (!in_array($socialNetwork, self::getSocialNetworks())) {
         $this->error = 'Social network is not valid...';
         $this->cwsDebug->error($this->error);
         exit;
     } elseif (!$this->isValidUrl($url)) {
         $this->error = 'URL is not valid...';
         $this->cwsDebug->error($this->error);
         exit;
     }
     $this->cwsDebug->titleH2('get ' . $socialNetwork . ' count');
     $this->cwsDebug->labelValue('URL', $url);
     $this->cwsCurl->reset();
     $this->cwsCurl->addOption(CURLOPT_HTTPHEADER, array('Content-type:application/json'));
     $apiUrl = null;
     switch ($socialNetwork) {
         case self::SN_DELICIOUS:
             $apiUrl = 'http://feeds.delicious.com/v2/json/urlinfo/data?url=' . urlencode($url);
             break;
         case self::SN_FACEBOOK:
             $apiUrl = 'http://api.facebook.com/method/links.getStats?urls=' . urlencode($url) . '&format=json';
             break;
         case self::SN_GOOGLEPLUS:
             $apiUrl = 'https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ';
             $this->cwsCurl->setPostMethod();
             $this->cwsCurl->addOption(CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
             break;
         case self::SN_LINKEDIN:
             $apiUrl = 'http://www.linkedin.com/countserv/count/share?url=' . urlencode($url);
             break;
         case self::SN_PINTEREST:
             $apiUrl = 'http://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url=' . urlencode($url);
             break;
         case self::SN_REDDIT:
             $apiUrl = 'http://buttons.reddit.com/button_info.json?url=' . urlencode($url);
             break;
         case self::SN_STUMBLEUPON:
             $apiUrl = 'http://www.stumbleupon.com/services/1.01/badge.getinfo?url=' . urlencode($url);
             break;
         case self::SN_TWITTER:
             $apiUrl = 'http://urls.api.twitter.com/1/urls/count.json?url=' . urlencode($url);
             break;
     }
     $this->cwsDebug->labelValue('API URL', $apiUrl, CwsDebug::VERBOSE_REPORT);
     $this->cwsCurl->setUrl($apiUrl);
     $this->cwsCurl->process();
     if ($this->cwsCurl->getError()) {
         $this->error = $this->cwsCurl->getError();
         $this->cwsDebug->error($this->error);
         return false;
     }
     $content = str_replace("\n", "", $this->cwsCurl->getContent());
     $this->cwsDebug->dump('Content fetched', $content, CwsDebug::VERBOSE_DEBUG);
     $json = $this->cwsCurl->getContent();
     switch ($socialNetwork) {
         case self::SN_DELICIOUS:
             if ($json == '[]') {
                 $json = '[{"total_posts": 0}]';
             }
         case self::SN_LINKEDIN:
             $json = str_replace('IN.Tags.Share.handleCount(', '', $json);
             $json = str_replace(');', '', $json);
             break;
         case self::SN_PINTEREST:
             $json = str_replace('receiveCount(', '', $json);
             $json = substr($json, 0, -1);
             break;
     }
     $json = json_decode($json, true);
     if ($json == null || $json === false) {
         $this->error = 'Invalid Json...';
         $this->cwsDebug->error($this->error);
         return false;
     }
     $this->cwsDebug->dump('Json', $json, CwsDebug::VERBOSE_REPORT);
     $result = false;
     switch ($socialNetwork) {
         case self::SN_DELICIOUS:
             if (isset($json[0]['total_posts'])) {
                 $result = intval($json[0]['total_posts']);
             }
             break;
         case self::SN_FACEBOOK:
             if (isset($json[0]['total_count'])) {
                 $result = intval($json[0]['total_count']);
             }
             break;
         case self::SN_GOOGLEPLUS:
             if (isset($json[0]['result']['metadata']['globalCounts']['count'])) {
                 $result = intval($json[0]['result']['metadata']['globalCounts']['count']);
             }
             break;
         case self::SN_LINKEDIN:
             if (isset($json['count'])) {
                 $result = intval($json['count']);
             }
             break;
         case self::SN_PINTEREST:
             if (isset($json['count'])) {
                 $result = intval($json['count']);
             }
             break;
         case self::SN_REDDIT:
             if (isset($json['data']['children'])) {
                 if (empty($json['data']['children'])) {
                     $result = 0;
                 } elseif (isset($json['data']['children'][0]['data']['score'])) {
                     $result = intval($json['data']['children'][0]['data']['score']);
                 }
             }
             break;
         case self::SN_STUMBLEUPON:
             if (isset($json['result']['views'])) {
                 $result = intval($json['result']['views']);
             }
             break;
         case self::SN_TWITTER:
             if (isset($json['count'])) {
                 $result = intval($json['count']);
             }
             break;
     }
     $this->cwsDebug->labelValue('Count', $result);
     return $result;
 }
示例#2
0
<?php

// Download CwsDump at https://github.com/crazy-max/CwsDump
require_once '../CwsDump/class.cws.dump.php';
$cwsDump = new CwsDump();
// Download CwsDebug at https://github.com/crazy-max/CwsDebug
require_once '../CwsDebug/class.cws.debug.php';
$cwsDebug = new CwsDebug($cwsDump);
$cwsDebug->setDebugVerbose();
$cwsDebug->setEchoMode();
// Download CwsCurl at https://github.com/crazy-max/CwsCurl
require_once '../CwsCurl/class.cws.curl.php';
$cwsCurl = new CwsCurl(new CwsDebug($cwsDump));
require_once 'class.cws.sharecount.php';
$cwsShareCount = new CwsShareCount($cwsDebug, $cwsCurl);
$result = $cwsShareCount->getAll('https://www.google.com/');