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

前端 未结 12 837
故里飘歌
故里飘歌 2020-12-22 20:04

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 a

相关标签:
12条回答
  • 2020-12-22 20:58

    This way you can get it with jquery. The div id="twitterCount" will be populated automatic when the page is loaded.

    
         function getTwitterCount(url){
             var tweets;
             $.getJSON('http://urls.api.twitter.com/1/urls/count.json?url=' + url + '&callback=?',             function(data){
             tweets = data.count;
             $('#twitterCount').html(tweets);
         });
         }
         var urlBase='http://http://stackoverflow.com';
         getTwitterCount(urlBase); 
    
    

    Cheers!

    0 讨论(0)
  • 2020-12-22 21:00

    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.

    0 讨论(0)
  • 2020-12-22 21:02

    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.

    0 讨论(0)
  • 2020-12-22 21:02

    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.

    0 讨论(0)
  • 2020-12-22 21:04

    This comment https://stackoverflow.com/a/8641185/1118419 proposes to use Topsy API. I am not sure that API is correct:

    Twitter response for www.e-conomic.dk:

    http://urls.api.twitter.com/1/urls/count.json?url=http://www.e-conomic.dk

    shows 10 count

    Topsy response fro www.e-conomic.dk:

    http://otter.topsy.com/stats.json?url=http://www.e-conomic.dk

    18 count

    0 讨论(0)
  • 2020-12-22 21:04

    This Javascript class will let you fetch share information from Facebook, Twitter and LinkedIn.

    Example of usage

    <p>Facebook count: <span id="facebook_count"></span>.</p>
    <p>Twitter count: <span id="twitter_count"></span>.</p>
    <p>LinkedIn count: <span id="linkedin_count"></span>.</p>
    <script type="text/javascript">
        var smStats=new SocialMediaStats('https://google.com/'); // Replace with your desired URL
        smStats.facebookCount('facebook_count'); // 'facebook_count' refers to the ID of the HTML tag where the result will be placed.
        smStats.twitterCount('twitter_count');
        smStats.linkedinCount('linkedin_count');    
    </script>
    

    Download

    https://404it.no/js/blog/SocialMediaStats.js

    More examples and documentation

    Javascript Class For Getting URL Shares On Facebook, Twitter And LinkedIn

    0 讨论(0)
提交回复
热议问题