How to count the number of twitter followers via php?

0

Good morning. I'm developing a blog and I need to list the number of followers a certain company has, but I can not do it. I was able to do the tanned listing of facebook but of twitter I can not at all

$xml = file_get_contents('http://twitter.com/users/show.xml?screen_name=cissamagazine'); if (preg_match('/followers_count>(.*)</', $xml, $array) != 0) {
$seguidores['count'] = $array[1];}echo $seguidores['count'];
    
asked by anonymous 16.04.2015 / 14:34

1 answer

2

By the Twitter API

Twitter has long closed down access to information and even limited requests to the developer community. You can verify that in the REST APIs of the 1.1 version (which is the current one) authentication is required to make any request, including the one you tried to do, to GET users/show .

I have a post detailed on my website about it. But basically the steps to reach your goal through the Application-only authentication method are these:

  • Create an application to get API key and Secret API ;
  • Through the keys, get the bearer token ;
  • token , perform the requested request;

Getting the bearer token :

$encoded_consumer_key = urlencode(CONSUMER_KEY); // API key
$encoded_consumer_secret = urlencode(CONSUMER_SECRET); // API secret
$bearer_token = $encoded_consumer_key . ':' . $encoded_consumer_secret;
$base64_consumer_key = base64_encode($bearer_token);
$url = "https://api.twitter.com/oauth2/token";
$headers = array(
        "POST /oauth2/token HTTP/1.1",
        "Host: api.twitter.com",
        "User-Agent: Twitter Application-only OAuth App",
        "Authorization: Basic " . $base64_consumer_key,
        "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
        "Content-Length: 29"
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$header = curl_setopt($ch, CURLOPT_HEADER, 1);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response = curl_exec ($ch);
curl_close($ch);
$output = explode("\n", $response);
$bearer_token = '';
foreach($output as $line) {
    if ($line !== false) {
        $bearer_token = $line;
    }
}
$bearer_token = json_decode($bearer_token);
$bearer_token = $bearer_token->{'access_token'};

When you then have token on hand, you can make your request for user information:

$url = "https://api.twitter.com/1.1/users/show.json";
$formed_url = '?screen_name=cissamagazine';
$headers = array(
        "GET /1.1/users/show.json" . $formed_url . " HTTP/1.1",
        "Host: api.twitter.com",
        "User-Agent: Twitter Application-only OAuth App",
        "Authorization: Bearer " . $bearer_token,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $formed_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

In the variable $response you will have a JSON , which you can manipulate it the way you want to get the user information.

    
16.04.2015 / 15:50