How to count the number of twitter followers via php?

Asked

Viewed 487 times

0

Good morning. I’m developing a blog and I need to list the number of followers that a certain company has, but I’m not able to do it. I managed to do the Facebook likes listing but Twitter 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'];
  • post a piece of code, some test result Voce did...

  • this was my last attempt $xml = file_get_contents('http://twitter.com/users/show.xml?screen_name=cissamagazine'); if (preg_match('/followers_count>(.*)</', $xml, $array) != 0) { $followers['Count'] = $array[1]; echo $followers['Count'];

  • 1

    @Gabrielschmidtcordeiro add this code in the question and not just in the comments ;)

  • Don’t use the Twitter API instead of scraping the page?

  • @Gabrielschmidtcorderly doesn’t that help you? http://www.pinceladasdaweb.com.br/blog/2009/10/19/show-numero-numberof followers/

  • 1

    @GWER

  • @Luiscipriani, I’ll see what I can do. But I think there must be something easier if compared to what I did to get the number of facebook likes

Show 2 more comments

1 answer

2

Through the Twitter API

It’s been a while since Twitter closed access to information and even limited requests to the developer community. You can check that in REST Apis of the version 1.1 (which is the current) authentication is required to perform any type of request, including for this one you tried to do, the GET users/show.

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

  • Create an application to obtain API key and API secret;
  • Through the keys, get the bearer token;
  • With the token, carry out the desired 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 then you have the token at hand, you can make your request to get 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, that you can manipulate it the way you want to get user information.

  • Paulo, I am a Twitter evangelist developer here in Brazil. Please let me know what kind of limitation you had with the API, we can help. Follower number information is public, there is no reason to limit and is available in any representation that returns the user entity, just as you show in your reply.

  • Luis, maybe I said it wrong. What I meant by "limited" refers to the limit of requests that can be made for a certain period of time, as shown in the documentation I quoted in my reply.

  • Yes, the rate limit actually exists to prevent abuse, but we can ease the boundaries for partners depending on the need. Just give it a tap via @Twitterdevbr. Abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.