【问题标题】:How to get Google +1 count for current page in PHP?如何在 PHP 中获取当前页面的 Google +1 计数?
【发布时间】:2012-01-13 15:56:23
【问题描述】:

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

【问题讨论】:

标签: php api google-api google-plus google-plus-one


【解决方案1】:

这个对我有用,比 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;
}

也可在此处查看推文、Pin 和 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 数字是点赞+分享的总和,有些人说加上 cmets(我还没有搜索过),无论如何使用你需要的那个。

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

希望有所帮助。

【讨论】:

  • Linked in 如何做到这一点?
  • @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']);
  • 我知道这是旧的,但我只是尝试了一下,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];
  • 这仍然有效,但显示了很多警告。而不是DOMDocument 我使用return preg_replace('/.*<div id="aggregateCount"[^>]+>(\d+)<\/div>.*/s', '$1', $html);
【解决方案2】:
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

【讨论】:

    【解决方案3】:

    此处其他帖子中列出的 cURL 和 API 方式不再有效。

    There is still at least 1 method,但它很丑,谷歌显然不支持它。您只需使用正则表达式从官方按钮的 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 的有效解决方案,请参阅我的答案进一步(stackoverflow.com/a/23088544/328272)。
    • 在需求量很大的情况下,他们不会为这样一个基本功能添加官方支持,这真是太荒谬了。
    【解决方案4】:

    到目前为止,下一个 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);
    

    【讨论】:

      【解决方案5】:

      Google 目前没有用于获取 URL 的 +1 计数的公共 API。您可以提交功能请求here。您也可以使用@DerVo 提到的reverse engineered 方法。请记住,尽管该方法可能随时更改和中断。

      【讨论】:

        【解决方案6】:

        我已组装此代码以直接从社交按钮使用的 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

        【讨论】:

          【解决方案7】:

          我必须合并来自不同选项和网址的一些想法才能让它为我工作:

          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...

          【讨论】:

            【解决方案8】:

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

            使用的技术与此处描述的技术相似,并且该库提供了一种机制来缓存检索到的数据。该库还有其他一些不错的功能:可通过 Composer 安装、经过全面测试、支持 HHVM。

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

            【讨论】:

              猜你喜欢
              • 2015-11-11
              • 2010-11-19
              • 2015-03-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多