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

前端 未结 12 853
故里飘歌
故里飘歌 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 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.

提交回复
热议问题