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

Joh*_*ohn 18 php api google-api google-plus-one google-plus

我想获得当前网页的Google + 1s数量?我想在PHP中执行此过程,然后将数量的共享或+ 1s写入数据库.这就是为什么,我需要它.那么,我怎样才能在PHP中完成这个过程(获得+ 1s的数量)?
提前致谢.

pau*_*agu 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;
}
Run Code Online (Sandbox Code Playgroud)

也适用于推文,针脚和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;
}
Run Code Online (Sandbox Code Playgroud)

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

这将适用于您的php设置允许打开外部URL,检查您的"allow_url_open"php设置.

希望有所帮助

  • 我知道这是旧的,但我只是尝试了它,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];``` (2认同)

Mil*_*age 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'] );
}
Run Code Online (Sandbox Code Playgroud)

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

来自internoetics.com


Luk*_*sna 6

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

仍然至少有一种方法,但它很难看,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;
}
Run Code Online (Sandbox Code Playgroud)


lme*_*urs 5

到目前为止,下一个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);
Run Code Online (Sandbox Code Playgroud)


abr*_*ham 2

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