skip to Main Content

I’m trying to access a single value of an array, I can’t seem to get it right. This is the code:

    $socialCounts = new socialNetworkShareCount(array(
    'url' => 'http://facebook.com/',
    'facebook' => true,
    'buffer' => true,
    'pinterest' => true,
    'linkedin' => true,
    'google' => true
));

 print_r($socialCounts->getShareCounts());

Which Returns the following:

{"facebookshares":52132062,"facebooklikes":0,"pinterestshares":243942,"linkedinshares":4708,"googleplusones":0,"buffershares":207477,"total":52588189}

How can I access the vaule of each individual Item? For example, if I’d like to echo the facebook shares value.

And this is the full class if you need:

class socialNetworkShareCount{

    public $shareUrl;
    public $socialCounts = array();
    public $facebookShareCount = 0;
    public $facebookLikeCount = 0;
    public $twitterShareCount = 0;
    public $bufferShareCount = 0;
    public $pinterestShareCount = 0;
    public $linkedInShareCount = 0;
    public $googlePlusOnesCount = 0;


    public function __construct($options){

        if(is_array($options)){
            if(array_key_exists('url', $options) && $options['url'] != ''){
                $this->shareUrl = $options['url'];
            }else{
                die('URL must be set in constructor parameter array!');
            }

            // Get Facebook Shares and Likes
            if(array_key_exists('facebook', $options)){
                $this->getFacebookShares();
                $this->getFacebookLikes();
            }

            // Get Twitter Shares
            if(array_key_exists('twitter', $options)){
                $this->getTwitterShares();
            }

            // Get Twitter Shares
            if(array_key_exists('pinterest', $options)){
                $this->getPinterestShares();
            }

            // Get Twitter Shares
            if(array_key_exists('linkedin', $options)){
                $this->getLinkedInShares();
            }

            // Get Twitter Shares
            if(array_key_exists('google', $options)){
                $this->getGooglePlusOnes();
            }

            // Get Buffer Shares
            if(array_key_exists('buffer', $options)){
                $this->getBufferShares();
            }

        }elseif(is_string($options) && $options != ''){
            $this->shareUrl = $options;

            // Get all Social Network share counts if they are not set individually in the options
            $this->getFacebookShares();
            $this->getFacebookLikes();
            $this->getTwitterShares();
            $this->getPinterestShares();
            $this->getLinkedInShares();
            $this->getGooglePlusOnes();
            $this->getBufferShares();

        }else{
            die('URL must be set in constructor parameter!');
        }
    }


    public function getShareCounts(){
        $totalShares = $this->getTotalShareCount($this->socialCounts);
        $this->socialCounts['total'] = $totalShares;




        return json_encode($this->socialCounts);
    }

    public function getTotalShareCount(array $shareCountsArray){
        return array_sum($shareCountsArray);
    }


    public function getFacebookShares(){
        $api = file_get_contents( 'http://graph.facebook.com/?id=' . $this->shareUrl );
        $count = json_decode( $api );
        if(isset($count->shares) && $count->shares != '0'){
            $this->facebookShareCount = $count->shares;
        }
        $this->socialCounts['facebookshares'] = $this->facebookShareCount;
        return $this->facebookShareCount;
    }

    public function getFacebookLikes(){
        $api = file_get_contents( 'http://graph.facebook.com/?id=' . $this->shareUrl );
        $count = json_decode( $api );
        if(isset($count->likes) && $count->likes != '0'){
            $this->facebookLikeCount = $count->likes;
        }
        $this->socialCounts['facebooklikes'] = $this->facebookLikeCount;
        return $this->facebookLikeCount;
    }

    public function getTwitterShares(){
        $api = file_get_contents( 'https://api.twitter.com/1.1/urls/count.json?url=' . $this->shareUrl );
        $count = json_decode( $api );
        if(isset($count->count) && $count->count != '0'){
            $this->twitterShareCount = $count->count;
        }
        $this->socialCounts['twittershares'] = $this->twitterShareCount;
        return $this->twitterShareCount;
    }


    public function getBufferShares(){
        $api = file_get_contents( 'https://api.bufferapp.com/1/links/shares.json?url=' . $this->shareUrl );
        $count = json_decode( $api );
        if(isset($count->shares) && $count->shares != '0'){
            $this->bufferShareCount = $count->shares;
        }
        $this->socialCounts['buffershares'] = $this->bufferShareCount;
        return $this->bufferShareCount;
    }


    public function getPinterestShares(){
        $api = file_get_contents( 'http://api.pinterest.com/v1/urls/count.json?callback%20&url=' . $this->shareUrl );
         $body = preg_replace( '/^receiveCount((.*))$/', '\1', $api );
         $count = json_decode( $body );
         if(isset($count->count) && $count->count != '0'){
            $this->pinterestShareCount = $count->count;
         }
         $this->socialCounts['pinterestshares'] = $this->pinterestShareCount;
         return $this->pinterestShareCount;
    }


    public function getLinkedInShares(){
         $api = file_get_contents( 'https://www.linkedin.com/countserv/count/share?url=' . $this->shareUrl . '&format=json' );
        $count = json_decode( $api );
        if(isset($count->count) && $count->count != '0'){
            $this->linkedInShareCount = $count->count;
        }
        $this->socialCounts['linkedinshares'] = $this->linkedInShareCount;
        return $this->linkedInShareCount;
    }

    public function getGooglePlusOnes(){

        if(function_exists('curl_version')){

            $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":"' . $this->shareUrl . '","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 );
            $this->googlePlusOnesCount = intval( $json[0]['result']['metadata']['globalCounts']['count'] );

        }else{

            $content = file_get_contents("https://plusone.google.com/u/0/_/+1/fastbutton?url=".urlencode($_GET['url'])."&count=true");
            $doc = new DOMdocument();
            libxml_use_internal_errors(true);
            $doc->loadHTML($content);
            $doc->saveHTML();
            $num = $doc->getElementById('aggregateCount')->textContent;

            if($num){
                $this->googlePlusOnesCount = intval($num);
            }
        }

        $this->socialCounts['googleplusones'] = $this->googlePlusOnesCount;
        return $this->googlePlusOnesCount;
    }}

2

Answers


  1. According to the class definition, you don’t need to invoke getShareCounts(). It looks like that method is perhaps used to provide some lower end API functionality. I don’t think it’s meant for your use case.

    All of the below public variables are accessible and filled in once the constructor has run.

    public $facebookShareCount = 0;
    public $facebookLikeCount = 0;
    public $twitterShareCount = 0;
    public $bufferShareCount = 0;
    public $pinterestShareCount = 0;
    public $linkedInShareCount = 0;
    public $googlePlusOnesCount = 0;
    

    So you can access them like so:

    $socialCounts = new socialNetworkShareCount(array(
        'url' => 'http://facebook.com/',
        'facebook' => true,
        'buffer' => true,
        'pinterest' => true,
        'linkedin' => true,
        'google' => true
    ));
    
    print_r($socialCounts->facebookShareCount); // facebook shares
    print_r($socialCounts->facebookLikeCount); // facebook likes
    
    Login or Signup to reply.
  2. mixed json_decode ( string $json [, bool $assoc = false [, int $depth
    = 512 [, int $options = 0 ]]] )

    Source.

    Since your json is an object, json_decode($yourparam) will return an stdClass by default, which will have public members. Since you would like to use an associated array instead, you need to call something like json_decode($yourparam, true), which will return an associated array, since the second parameter is a boolean value determined by your intention whether you want an associated array as result, default value being false.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search