如何在PHP中获得当前页面的Google+1计数


How to get Google +1 count for current page in PHP?

我想获得当前网页的Google+1s计数?我想在PHP中完成这个过程,然后将共享数或+1s写入数据库。这就是为什么,我需要它。那么,我如何在PHP中完成这个过程(获得+1s的计数)
提前谢谢。

这个对我有效,比CURL的更快:

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;
}

这里也有推特,别针和Facebook

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的数字是点赞+分享的总和,有些人说加上评论(我还没有搜索),无论如何都要使用你需要的数字。

如果您的php设置允许打开外部url,请检查您的"allow_url_open"php设置。

希望有帮助。

function get_plusones($url) {
  $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":"' . $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);
  $json = json_decode($curl_results, true);
  return intval( $json[0]['result']['metadata']['globalCounts']['count'] );
}

echo get_plusones("http://www.stackoverflow.com")

来自internoetics.com

其他文章中列出的cURL和API方式不再有效

至少还有一个方法,但它很难看,谷歌显然不支持它。你只需从JavaScript源代码中提取一个变量,用一个正则表达式:

function shinra_gplus_get_count( $url ) {
    $contents = file_get_contents( 
        'https://plusone.google.com/_/+1/fastbutton?url=' 
        . urlencode( $url ) 
    );
    preg_match( '/window'.__SSR = {c: (['d]+)/', $contents, $matches );
    if( isset( $matches[0] ) ) 
        return (int) str_replace( 'window.__SSR = {c: ', '', $matches[0] );
    return 0;
}

到目前为止,下一个PHP脚本在检索Google+的股票和+1的数量方面非常有效。

$url = 'http://nike.com';
$gplus_type = true ? 'shares' : '+1s';
/**
 * Get Google+ shares or +1's.
 * See out post at stackoverflow.com/a/23088544/328272
 */
function get_gplus_count($url, $type = 'shares') {
  $curl = curl_init();
  // According to stackoverflow.com/a/7321638/328272 we should use certificates
  // to connect through SSL, but they also offer the following easier solution.
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  if ($type == 'shares') {
    // Use the default developer key AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ, see
    // tomanthony.co.uk/blog/google_plus_one_button_seo_count_api.
    curl_setopt($curl, CURLOPT_URL, 'https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ');
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, 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($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
  }
  elseif ($type == '+1s') {
    curl_setopt($curl, CURLOPT_URL, 'https://plusone.google.com/_/+1/fastbutton?url='.urlencode($url));
  }
  else {
    throw new Exception('No $type defined, possible values are "shares" and "+1s".');
  }
  $curl_result = curl_exec($curl);
  curl_close($curl);
  if ($type == 'shares') {
    $json = json_decode($curl_result, true);
    return intval($json[0]['result']['metadata']['globalCounts']['count']);
  }
  elseif ($type == '+1s') {
    libxml_use_internal_errors(true);
    $doc = new DOMDocument();
    $doc->loadHTML($curl_result);
    $counter=$doc->getElementById('aggregateCount');
    return $counter->nodeValue;
  }
}
// Get Google+ count.
$gplus_count = get_gplus_count($url, $gplus_type);

Google目前没有用于获取URL+1计数的公共API。您可以在此处提交功能请求。你也可以使用@DerVo提到的逆向工程方法。请记住,尽管这种方法随时可能改变和中断。

我已经组装了这段代码,以便直接从social按钮使用的iframe中读取count。我还没有对它进行大规模测试,所以也许你必须放慢请求和/或更改用户代理:)。这是我的工作代码:

function get_plusone($url) 
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://plusone.google.com/_/+1/fastbutton?   
bsv&size=tall&hl=it&url=".urlencode($url));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$html = curl_exec ($curl);
curl_close ($curl);
$doc = new DOMDocument();
$doc->loadHTML($html);
$counter=$doc->getElementById('aggregateCount');
return $counter->nodeValue;

}

用法如下:

echo get_plusones('http://stackoverflow.com/');

结果是:3166

我不得不合并来自不同选项和URL的一些想法,以使其适用于我:

function getPlusOnes($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "https://plusone.google.com/_/+1/fastbutton?url=".urlencode($url));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        $html = curl_exec ($curl);
        curl_close ($curl);
        $doc = new DOMDocument();
        $doc->loadHTML($html);
        $counter=$doc->getElementById('aggregateCount');
        return $counter->nodeValue;
    }

我所要做的就是更新网址,但我想为那些感兴趣的人发布一个完整的选项。

echo getPlusOnes('http://stackoverflow.com/')

感谢Cardy使用这种方法,然后我只需要获得一个对我有用的url…

我发布了一个PHP库,用于检索主要社交网络的计数。它目前支持谷歌、脸书、推特和Pinterest。

所使用的技术与这里描述的技术类似,并且库提供了缓存检索到的数据的机制。这个库还有一些其他不错的功能:可以通过Composer安装,经过充分测试,支持HHVM。

http://dunglas.fr/2014/01/introducing-the-socialshare-php-library/