Is there a way to get the twitter share count for

2019-01-29 23:44发布

I looked through the API documentation but couldn't find it. It would be nice to grab that number to see how popular a url is. Engadget uses the twitter share button on articles if you're looking for an example. I'm attempting to do this through javascript. Any help is appreciated.

标签: twitter
12条回答
Rolldiameter
2楼-- · 2019-01-30 00:24

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

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-30 00:25

I just read the contents into a json object via php, then parse it out..

<script>
<?php
$tweet_count_url = 'http://urls.api.twitter.com/1/urls/count.json?url='.$post_link;
$tweet_count_open = fopen($tweet_count_url,"r");
$tweet_count_read = fread($tweet_count_open,2048);
fclose($tweet_count_open);
?>
var obj = jQuery.parseJSON('<?=$tweet_count_read;?>'); 
jQuery("#tweet-count").html("("+obj.count+") "); 
</script>

Simple enough, and it serves my purposes perfectly.

查看更多
Deceive 欺骗
4楼-- · 2019-01-30 00:28

I know that is an old question but for me the url http://cdn.api.twitter.com/1/urls/count.json?url=http://stackoverflow.com did not work in ajax calls due to Cross-origin issues.

I solved using PHP CURL, I made a custom route and called it through ajax.

  /* Other Code */
  $options = array(
    CURLOPT_RETURNTRANSFER => true,   // return web page
    CURLOPT_HEADER         => false,  // don't return headers
    CURLOPT_FOLLOWLOCATION => true,   // follow redirects
    CURLOPT_MAXREDIRS      => 10,     // stop after 10 redirects
    CURLOPT_ENCODING       => "",     // handle compressed
    CURLOPT_USERAGENT      => "test", // name of client
    CURLOPT_AUTOREFERER    => true,   // set referrer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,    // time-out on connect
    CURLOPT_TIMEOUT        => 120,    // time-out on response
);
$url = $_POST["url"]; //whatever you need
if($url !== ""){
    $curl = curl_init("http://urls.api.twitter.com/1/urls/count.json?url=".$url);
    curl_setopt_array($curl, $options);
    $result = curl_exec($curl);
    curl_close($curl);
    echo json_encode(json_decode($result)); //whatever response you need
}

It is important to use a POST because passsing url in GET request cause issues.

Hope it helped.

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-30 00:36

Yes, there is. As long as you do the following:

  1. Issue a JSONP request to one of the urls:

    http://cdn.api.twitter.com/1/urls/count.json?url=[URL_IN_REQUEST]&callback=[YOUR_CALLBACK]

    http://urls.api.twitter.com/1/urls/count.json?url=[URL_IN_REQUEST]&callback=[YOUR_CALLBACK]

  2. Make sure that the request you are making is from the same domain as the [URL_IN_REQUEST]. Otherwise, it will not work.

    Example:

    Making requests from example.com to request the count of example.com/page/1. Should work.

    Making requests from another-example.com to request the count of example.com/page/1. Will NOT work.

查看更多
做个烂人
6楼-- · 2019-01-30 00:37

The approved reply is the right one. There are other versions of the same endpoint, used internally by Twitter.

For example, the official share button with count uses this one:

https://cdn.syndication.twitter.com/widgets/tweetbutton/count.json?url=[URL]

JSONP support is there adding &callback=func.

查看更多
欢心
7楼-- · 2019-01-30 00:43

No.

How do I access the count API to find out how many Tweets my URL has had?

In this early stage of the Tweet Button the count API is private. This means you need to use either our javascript or iframe Tweet Button to be able to render the count. As our systems scale we will look to make the count API public for developers to use.

http://dev.twitter.com/pages/tweet_button_faq#custom-shortener-count

查看更多
登录 后发表回答