PHP Function: Menghitung Jumlah Sosial Media Sharing, Melalui URL

 Author : hadinug, 23 October, 2014, Komentar

sosial count


Penggunaan widget media sosial seperti Facebook, twitter, google+ dll, sekarang ini menjadi hal yang wajib ada disetiap website yang ada sekarang ini. Namun penggunaan widget dinilai kurang begitu baik, mengingat waktu loading yang dibutuhkan untuk menampilkan widget tersebut relative lama. Seperti penggunaan widget untuk menampilkan jumlah sahare link website di sosial media.



Nah berangkat dari permasalah tadi, terdapat banyak cara bagaimana mendapatkan jumlah sosial sharing website yang kita miliki dengan function PHP. 


Google+

 function ca_gplus_shares($url) {
        // G+ DATA
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, 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"}]');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result, true);
    } 


All Social Count


 function ca_social_count($url = null) {
        if ($url != null && is_url($url)) {
            $finfo = json_decode(file_get_contents('http://api.ak.facebook.com/restserver.php?v=1.0&method=links.getStats&urls=' . $url . '&format=json'));
            $tinfo = json_decode(file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url));
            $dinfo = json_decode(file_get_contents('http://feeds.delicious.com/v2/json/urlinfo/data?url=' . $url));
            $linfo = json_decode(file_get_contents('http://www.linkedin.com/countserv/count/share?url=' . $url . '&format=json'));
            $pinfo = json_decode(preg_replace('/^receiveCount((.*))$/', "1", file_get_contents('http://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url=' . $url)));
            $gplus = ca_gplus_shares($url);

            $output = array(
                'facebook' => isset($finfo[0]) ? $finfo[0]->total_count : NULL,
                'twitter' => isset($tinfo->count) ? $tinfo->count : NULL,
                'delicious' => isset($dinfo[0]) ? $dinfo[0]->total_posts : NULL,
                'linkedin' => isset($linfo) ? $linfo->count : NULL,
                'pinterest' => isset($pinfo->count) ? $pinfo->count : NULL,
                'googlePlus' => isset($gplus[0]['result']) ? $gplus[0]['result']['metadata']['globalCounts']['count'] : NULL
            );

            return $output;
        }
    }


Facebook Only


function ca_fb($url) {
        $fql = "SELECT url, normalized_url, share_count, like_count, comment_count, ";
        $fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";
        $fql .= "link_stat WHERE url = '$url'";

        $apifql = "http://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql);
        $json = file_get_contents($apifql);
        return json_decode($json)[0];
    }


Untuk lebih menghemat load website, kita bisa memaksimalkan penggunaan ajax. Lihat demo dan download aplikasinya disini


 
Bagikan halaman ke sosial media
 
Tags :   facebook    fql    social       

Tentang Penulis

writer hadinug
Web Developer di PT. Yafii Solusi Internasional

Link: http://ysi.co.id

   Komentar Anda