DEV Community

manuel
manuel

Posted on

How to find out who don't follow back with Twitter's REST Api.

I wrote this few hours ago to show you, how I track my unfollower on Twitter with PHP and Git.
Here is another interesting thing you can do with Twitter's REST Api. Find out who don't follow back.

Based on my other post, here an example how I do this:

<?php
require_once 'env.php';
require "vendor/autoload.php";

use Abraham\TwitterOAuth\TwitterOAuth;
use Tightenco\Collect\Support\Collection;

$api       = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$parameter = [
    'screen_name' => 'mnlwldr',
    'count'       => 5000,
];

/* Get a list of IDs who follow me */
$follower   = $api->get("followers/ids", $parameter);

/* Get a list of IDs I follow */
$followings = $api->get("friends/ids", $parameter);

collect($followings->ids)

    /* diff the two arrays and return the difference */
    ->diff($follower->ids)

    /* chunk the diff because Twitter users/lookup accept max 100 IDs per request */
    ->chunk(100)

    /* now iterate over the chunks and call the users/lookup endpoint */
    ->flatMap(function (Collection $ids) use ($api) {
        return $api->get('users/lookup', [
            'user_id' => $ids->implode(','),
        ]);
    })
    /* iterate over the result and print the username */
    ->each(function ($profile) {
        print $profile->screen_name . PHP_EOL;
    });
Enter fullscreen mode Exit fullscreen mode

Top comments (0)