如何在 PHP 中获取当前页面的 Google +1 计数? [英] How to get Google +1 count for current page in PHP?

查看:25
本文介绍了如何在 PHP 中获取当前页面的 Google +1 计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取当前网页的 Google +1 计数?我想在 PHP 中执行此过程,然后将共享数或 +1 数写入数据库.这就是为什么,我需要它.那么,如何在 PHP 中执行此过程(获取 +1 的计数)?
提前致谢.

I want to get count of Google +1s for current web page ? I want to do this process in PHP, then write number of shares or +1s to database. That's why, I need it. So, How can I do this process (getting count of +1s) in PHP ?
Thanks in advance.

推荐答案

这个对我有用并且比 CURL 更快:

This one works for me and is faster than the CURL one:

function getPlus1($url) {
    $html =  file_get_contents( "https://plusone.google.com/_/+1/fastbutton?url=".urlencode($url));
    $doc = new DOMDocument();   $doc->loadHTML($html);
    $counter=$doc->getElementById('aggregateCount');
    return $counter->nodeValue;
}

这里也有推文、Pins 和 Facebooks

also here for Tweets, Pins and Facebooks

function getTweets($url){
    $json = file_get_contents( "http://urls.api.twitter.com/1/urls/count.json?url=".$url );
    $ajsn = json_decode($json, true);
    $cont = $ajsn['count'];
    return $cont;
}

function getPins($url){
    $json = file_get_contents( "http://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url=".$url );
    $json = substr( $json, 13, -1);
    $ajsn = json_decode($json, true);
    $cont = $ajsn['count'];
    return $cont;
}

function getFacebooks($url) { 
    $xml = file_get_contents("http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($url));
    $xml = simplexml_load_string($xml);
    $shares = $xml->link_stat->share_count;
    $likes  = $xml->link_stat->like_count;
    $comments = $xml->link_stat->comment_count; 
    return $likes + $shares + $comments;
}

注意:Facebook的数字是喜欢+分享的总和,有些人说加评论(我还没搜索过),反正用你需要的.

Note: Facebook numbers are the sum of likes+shares and some people said plus comments (I didn't search this yet), anyway use the one you need.

如果您的 php 设置允许打开外部 url,这将起作用,请检查您的allow_url_open"php 设置.

This will works if your php settings allow open external url, check your "allow_url_open" php setting.

希望有所帮助.

这篇关于如何在 PHP 中获取当前页面的 Google +1 计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆