Is there a way to get the twitter share count for a specific URL?

You can use the following API endpoint,

http://cdn.api.twitter.com/1/urls/count.json?url=http://stackoverflow.com

Note that the http://urls.api.twitter.com/ endpoint is not public.)

The endpoint will return a JSON string similar to,

{"count":27438,"url":"http:\/\/stackoverflow.com\/"}

On the client, if you are making a request to get the URL share count for your own domain (the one the script is running from), then an AJAX request will work (e.g. jQuery.getJSON). Otherwise, issue a JSONP request by appending callback=?:

jQuery.getJSON('https://cdn.api.twitter.com/1/urls/count.json?url=http://stackoverflow.com/&callback=?', function (data) {
    jQuery('#so-url-shares').text(data.count);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="so-url-shares">Calculating...</div>

Update:

As of 21st November 2015, this way of getting twitter share count, does not work anymore. Read more at: https://blog.twitter.com/2015/hard-decisions-for-a-sustainable-platform

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)