Handling Errors in Auto Follow Back on Twitter

I revised the code posted in How to Auto Follow on Twitter by Twitter API v1.1 & PHP. The new version handles possible errors occurred in auto follow back.

New Code

<?php
require __DIR__ . '/lib/twitteroauth/twitteroauth.php';

define('CONSUMER_KEY', 'your_consumer_key');
define('CONSUMER_SECRET', 'your_consumer_secret');
define('ACCESS_TOKEN', 'your_access_token');
define('ACCESS_TOKEN_SECRET', 'your_access_token_secret');

$toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

$followers = $toa->get('followers/ids', array('cursor' => -1));
$friends   = $toa->get('friends/ids',   array('cursor' => -1));

if (!is_array($followers->ids) or !is_array($friends->ids)) exit;

$errorIds = file_exists('error.log') ? file_get_contents('error.log') : null;
$errCnt = 0;

foreach ($followers->ids as $i => $id)
{
    if (empty($friends->ids) or !in_array($id, $friends->ids))
    {
        if (!is_null($errorIds) and false !== strpos($errorIds, (string)$id)) continue;

        $ret = $toa->post('friendships/create', array('user_id' => $id));

        if (isset($ret->errors))
        {
            /*
               Error code:
                 34 : Sorry, that page does not exist
                 108: Cannot find specified user
                 130: Over capacity
                 131: Internal error
                 159: Sorry, this account has been suspended
                 160: You've already requested to follow USER_NAME
                 161: You are unable to follow more people at this time
            */
            if ($ret->errors[0]->code != 161 and $ret->errors[0]->code != 131 and $ret->errors[0]->code != 130)
            {
                $fh = fopen('error.log', 'a');
                fwrite($fh, "$id\n");
                fclose($fh);
            }

            $errCnt++;
            if ($errCnt >= 5) break;
        }
    }
}

Changes

if (!is_array($followers->ids) or !is_array($friends->ids)) exit;

I added the code at line 14 that checks if $followers and $friends lists were successfully returned, otherwise exits the code. Because a get request can fail when a Twitter API limit reached or an internal server error occurred.

A rare situation can happens, that a get request for $followers (users following you) was successful but not for $friends (users you are following). In this case, the original code were trying to request unnecessary follow backs for users listed in $followers because your $friends list was null, even though you were already following them.

In order to avoid this situation, I added the code at line 14.

Next, the codes from line 25:

        $ret = $toa->post('friendships/create', array('user_id' => $id));

        if (isset($ret->errors))
        {
            /*
               Error code:
                 34 : Sorry, that page does not exist
                 108: Cannot find specified user
                 130: Over capacity
                 131: Internal error
                 159: Sorry, this account has been suspended
                 160: You've already requested to follow USER_NAME
                 161: You are unable to follow more people at this time
            */
            if ($ret->errors[0]->code != 161 and $ret->errors[0]->code != 131 and $ret->errors[0]->code != 130)
            {
                $fh = fopen('error.log', 'a');
                fwrite($fh, "$id\n");
                fclose($fh);
            }

            $errCnt++;
            if ($errCnt >= 5) break;
        }

The code above was added to handle possible errors in follow back request.

        $ret = $toa->post('friendships/create', array('user_id' => $id));

The following errors can occur at line 25:

  • error code 34 : Sorry, that page does not exist
  • error code 108: Cannot find specified user
  • error code 130: Over capacity
  • error code 131: Internal error
  • error code 159: Sorry, this account has been suspended
  • error code 160: You’ve already requested to follow USER_NAME
    (occurs where a user account is private and a friend (follow) request is not accepted)
  • error code 161: You are unable to follow more people at this time

When the errors occurred, except for error code 130, 131 and 161, a user ID is saved in “error.log” in order to avoid sending a request again for the user.

This is needed because, for example, $toa->get('followers/ids', array('cursor' => -1)); can return a list including a user ID which causes “error code 108: Cannot find specified user”. I don’t know why Twitter API returns a user ID not existing, but actually it can happen, so I needed to add the error handling code.

Finally, the code at 46-47 breaks the loop for follow backs when errors occurred more than 5 times for just in case.