// FOLLOW BACK FOLLOWERS
// Get followers ids
$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?screen_name=EmergencyPipou';
$requestMethod = 'GET';
$followers = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
$followers = json_decode($followers);
$followerIds = array();
foreach ($followers->ids as $i => $id) {
    $followerIds[] = $id;
}
// Get followers list
$url = 'https://api.twitter.com/1.1/followers/list.json';
$getfield = '?screen_name=EmergencyPipou';
$requestMethod = 'GET';
$followersList = $twitter->resetFields()->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
$followersList = json_decode($followersList);
$followersList = $followersList->users;
// Get friends
$url = 'https://api.twitter.com/1.1/friends/ids.json';
$getfield = '?screen_name=EmergencyPipou';
$requestMethod = 'GET';
$friends = $twitter->resetFields()->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
$friends = json_decode($friends);
$friendIds = array();
foreach ($friends->ids as $i => $id) {
    $friendIds[] = $id;
}
// Follow followers that are not friends
foreach ($followerIds as $id) {
    if (!in_array($id, $friendIds)) {
 $requestURL = 'http://api.art.rmngp.fr:80/v1/works?exists=images%2Ctitle.fr&per_page=1&page=' . $randomPage;
 $result = getCURLOutput($requestURL, $RmnAPIKey);
 if (isset($result->hits) && count($result->hits->hits) > 0) {
     $work = $result->hits->hits[0]->_source;
     // Upload image of the work
     foreach ($work->images as $image) {
         if ($image->default) {
             $photographerCredit = $image->photographer->name;
             if (isset($image->permalink)) {
                 $permalink = $image->permalink;
             }
             $imageContent = file_get_contents($image->urls->original);
             $imageData = base64_encode($imageContent);
             $postfields = array('media_data' => $imageData);
             $requestMethod = "POST";
             $response = $twitter->resetFields()->buildOauth($mediaUploadURL, $requestMethod)->setPostfields($postfields)->performRequest();
             $response = json_decode($response);
             if (isset($response->media_id_string)) {
                 $mediaId = $response->media_id_string;
             }
         }
     }
     if (!isset($mediaId)) {
         return false;
     }
     // Build tweet
     // Set max string length based on Twitter config
     $maxLength = 140 - $twitterConfig->characters_reserved_per_media - strlen(' / ' . $photographerCredit);
     if (isset($permalink)) {
         $maxLength -= $twitterConfig->short_url_length + 1;
     }