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

18

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


1
谷歌一下。https://developers.google.com/+/plugins/+1button/ - Josh
2
http://johndyer.name/getting-counts-for-twitter-links-facebook-likesshares-and-google-1-plusones-in-c-or-php/ - DerVO
@DerVO 这段代码不起作用。 - John
2
@John:也许这里可以找到答案?http://stackoverflow.com/questions/7321202/difficulty-getting-google-plus-one-count - DerVO
JaiV 问道:“是否仍然没有获取URL的+1计数的选项?” - senderle
显示剩余2条评论
8个回答

19

这个方法对我很有效,而且比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;
}

还可以在推特、Pinterest和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设置。
希望能帮到您。

你如何在领英上做到这一点? - Chill Web Designs
1
@ChillWebDesigns从我的代码中抄袭,没有整齐的格式,但你应该能理解:$stream = @file_get_contents("http://www.linkedin.com/countserv/count/share?url={$url}&format=json"); $json = json_decode($stream, true); $results['linkedin'] = intval($json['count']); - Ben
2
我知道这是老的了,但是我刚试过loadHtml在文档中有svg标签的时候会有问题。下面是我的快速解决方案:$html = file_get_contents( "https://plusone.google.com/_/+1/fastbutton?url=".urlencode($url)); $html = explode('<div id="aggregateCount" class="Oy">', $html)[1]; return explode('</div>', $html)[0]; - Anthony
这仍然可以工作,但会显示很多警告。我使用return preg_replace('/.*<div id="aggregateCount"[^>]+>(\d+)<\/div>.*/s', '$1', $html);代替DOMDocument - Dominik Späte

12
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


6

在此处列出的cURL和API方法已不再适用。

仍有至少1种方法,但它很丑陋,而且Google显然不支持它。你只需使用正则表达式从官方按钮的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;
}

这个回答似乎暗示了使用cURL + API的方法不再起作用,但是目前我已经有一个工作解决方案,使用这些技术来检索Google+分享和+1,详见我的进一步回答(https://dev59.com/Xmox5IYBdhLWcg3w95E9#23088544)。 - lmeurs
1
当有如此巨大的需求时,他们不会为这样一个基本功能添加官方支持,这真是荒谬的。 - Jason Champion

5
下面的PHP脚本可以很好地检索分享和+1上的Google+计数。
$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);

2

我已经编写了这段代码,可以直接从社交按钮使用的iframe中读取计数。由于我还没有进行大规模测试,所以您可能需要减慢请求速度和/或更改用户代理 :) 。这是我的工作代码:

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


2

谷歌目前没有公开的API可以获取URL的+1计数。您可以在此处提交功能请求。您也可以使用由@DerVo提到的逆向工程方法。但请记住,该方法可能会随时更改并停止运作。


1

我不得不从不同的选项和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;
    }

我只需要更新URL,但我想为那些感兴趣的人提供完整的选项。

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

感谢Cardy使用这种方法,然后我只需要获取一个适合我的URL即可...

0

我发布了一个 PHP 库,用于检索主要社交网络的计数。目前支持 Google、Facebook、Twitter 和 Pinterest。

所使用的技术类似于此处描述的技术,并且该库提供了一种缓存检索到的数据的机制。这个库还有一些其他不错的特性:可通过 Composer 安装,完全测试过,支持 HHVM。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接