Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 405
John Avatar asked Jan 13 '12 15:01

John


People also ask

What is$ count in PHP?

PHP count() function is an in-built function available in PHP, which counts and returns the number of elements in an array. It also counts the number of properties in an object. The count() function may return 0 for the variable, which has been declared with an empty array or for the variable which is not set.

How to array count in PHP?

How to Count all Elements or Values in an Array in PHP. We can use the PHP count() or sizeof() function to get the particular number of elements or values in an array. The count() and sizeof() function returns 0 for a variable that we can initialize with an empty array.

Which function returns the number of element in array?

The count() function returns the number of elements in an array.


4 Answers

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

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

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.

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

Hope helps.

like image 128
paulgagu Avatar answered Nov 07 '22 05:11

paulgagu


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")

from internoetics.com

like image 24
Milindu Sanoj Kumarage Avatar answered Nov 07 '22 07:11

Milindu Sanoj Kumarage


The cURL and API way listed in the other posts here no longer works.

There is still at least 1 method, but it's ugly and Google clearly doesn't support it. You just rip the variable out of the JavaScript source code for the official button with a regular expression:

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;
}
like image 43
Luke Mlsna Avatar answered Nov 07 '22 07:11

Luke Mlsna


The next PHP script works great so far for retrieving Google+ count on shares and +1's.

$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);
like image 26
lmeurs Avatar answered Nov 07 '22 05:11

lmeurs