如何为facebook,twitter和google plus制作个人分享按钮

时间:2013-11-13 14:40:33

标签: facebook button twitter share

像这样:

enter image description here

参见文章下的exmaple:

http://www.golem.de/news/server-prozessor-intel-bestaetigt-broadwell-als-xeon-fuer-sockel-1150-1311-102622.html

设计不是问题,但是如何为twitter,facebook和google plus获取此网址的份额?

1 个答案:

答案 0 :(得分:0)

找到解决方案。以下是如何通过API获取计数:

    <?php   

if ($network == 'facebook')
{
    $string = file_get_contents('http://graph.facebook.com/?id=' . $current_url);
    $array = json_decode($string, true);
    if (isset($array['shares']))
        $number = intval($array['shares']);
    else
        $number = 0;    
}
elseif ($network == 'twitter')
{
    $string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $current_url);
    $array = json_decode($string, true);
    if (isset($array['count']))
        $number = intval($array['count']);
    else
        $number = 0;        
}
elseif ($network == 'googleplus')
{
    $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":"'.$current_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);
    $array = json_decode($curl_results, true);
    if (isset($array[0]['result']['metadata']['globalCounts']['count']))
        $number = intval($array[0]['result']['metadata']['globalCounts']['count']);
    else
        $number = 0;
}

?>