Skip to content

Instantly share code, notes, and snippets.

@ihorvorotnov
Last active May 26, 2024 08:13
Show Gist options
  • Save ihorvorotnov/9132596 to your computer and use it in GitHub Desktop.
Save ihorvorotnov/9132596 to your computer and use it in GitHub Desktop.
Get number of shares from social platforms
Facebook*:
https://api.facebook.com/method/links.getStats?urls=%%URL%%&format=json
+ works, returns shares, likes, comments and total
Twitter:
http://urls.api.twitter.com/1/urls/count.json?url=%%URL%%&callback=twttr.receiveCount
+ v1 API but still works
Reddit:
http://buttons.reddit.com/button_info.json?url=%%URL%%
+ works
LinkedIn:
http://www.linkedin.com/countserv/count/share?url=%%URL%%&format=json
+ works
Digg:
http://widgets.digg.com/buttons/count?url=%%URL%%
- NOT WORKING!
Delicious:
http://feeds.delicious.com/v2/json/urlinfo/data?url=%%URL%%
+ works
StumbleUpon:
http://www.stumbleupon.com/services/1.01/badge.getinfo?url=%%URL%%
+ works
Pinterest:
http://widgets.pinterest.com/v1/urls/count.json?source=6&url=%%URL%%
+ works
Vkontakte Shares:
http://vk.com/share.php?act=count&url=%%URL%%
+ works
Vkontakte Likes:
http://suvitruf.ru/2012/06/02/1255/
Odnoklassniki:
http://www.odnoklassniki.ru/dk?st.cmd=extOneClickLike&uid=odklocs0&ref=%%URL%%
+ works
Мой Мир Mail.ru:
http://connect.mail.ru/share_count?url_list=%%URL%%
+ works
Google+ counts are retrieved via a JSON-RPC POST call.
POST URL:
https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ
POST Body:
[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"%%URL%%","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]
or:
https://plusone.google.com/u/0/_/+1/fastbutton?count=true&url=%%URL%%
+ shows counter, you can parse html (div#aggregateCount)
$google_request = file_get_contents('https://plusone.google.com/u/0/_/+1/fastbutton?count=true&url='.$url);
$plusone_count = preg_replace('/(.*)<div id="aggregateCount" class="t1">(([0-9])*)<\/div>(.*)/is','$2',$google_request);
Reading:
http://www.tomanthony.co.uk/blog/google_plus_one_button_seo_count_api/
http://sergunik.name/?p=799
http://suvitruf.ru/2012/06/02/1255/
https://gist.github.com/jonathanmoore/2640302
@nanangkoesharwanto
Copy link

nanangkoesharwanto commented Aug 29, 2016

https://api.facebook.com/method/links.getStats?urls=https://www.youtube.com/watch?v=-Fnh_-boj1o&format=json

{"error_code":12,"error_msg":"REST API is deprecated for versions v2.1 and higher (12)","request_args":[{"key":"method","value":"links.getStats"},{"key":"urls","value":"https:\/\/www.youtube.com\/watch?v=-Fnh_-boj1o"},{"key":"format","value":"json"}]}

@faeton
Copy link

faeton commented Nov 13, 2016

Facebook now works this way:
http://graph.facebook.com/$URL
Then parse for share->share_count

@faeton
Copy link

faeton commented Nov 13, 2016

Twitter doesnt officially work.
One other way is to use 'http://opensharecount.com/count.json?url='.$url

@ElchinIsmayilov
Copy link

Facebook new share count api: http://graph.facebook.com/?id=your_url
Twitter share count is removed, so no way to get it, don't try it.

@DonPramis
Copy link

DonPramis commented Oct 23, 2017

Guys need some Advice. Please Check my code if correct... I cant make it work:

https://graph.facebook.com/v2.1/?id=http://google.com <<<

/* Get Facebook Data
	-------------------------------------------------- */
		public function getFacebookData($domain)
		{
			try
			{
				$callback_url = "https://graph.facebook.com/v2.1/?";
				$data = array(
					'id' => "SELECT share_count, like_count, comment_count FROM link_stat WHERE url='$domain'"
				);

				$curl_response = $this->curl->get($callback_url . http_build_query($data, '', '&'));

				if ($curl_response->headers['Status-Code'] == "200") {

					$parse_response = json_decode($curl_response, true);
					$fb_share_count = $parse_response['data'][0]['share_count'];
					$fb_like_count = $parse_response['data'][0]['like_count'];
					$fb_comment_count = $parse_response['data'][0]['comment_count'];

				} else {
					$fb_share_count = 0;
					$fb_like_count = 0;
					$fb_comment_count = 0;
				}

				$response = array(
					'status' => 'success',
					'data' => array(
						'fb_share_count' => filter_var($fb_share_count, FILTER_SANITIZE_NUMBER_INT),
						'fb_like_count' => filter_var($fb_like_count, FILTER_SANITIZE_NUMBER_INT),
						'fb_comment_count' => filter_var($fb_comment_count, FILTER_SANITIZE_NUMBER_INT)
					)
				);

			}
			catch (Exception $e)
			{
				$response = array(
					'status' => 'error',
					'msg' => $e->getMessage()
				);
			}
			return $response;
		}

@DonPramis
Copy link

@ElchinIsmayilov check please my code..still cant get a share from facebook

@caiobatis
Copy link

Hi, does anyone have a solution for the Twitter counter?

@1v
Copy link

1v commented Mar 4, 2018

ok.ru:

https://connect.ok.ru/dk?st.cmd=extLike&tp=json&ref=https://ya.ru/

@pagelab
Copy link

pagelab commented Aug 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment