<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/twitterCredentials.php';

use mpyw\Co\Co;
use mpyw\Co\CURLException;
use mpyw\Cowitter\Client;
use mpyw\Cowitter\HttpException;

// Create a client object
	   $client = new Client([
           tweeterCK,
           tweeterCS,
           tweeterAT,
           tweeterAS,
       ]);

//'screen_name' => 'RHCJO', //His Majesty King Abdullah II 
//'screen_name' => 'AlsisiOfficial', //Abdelfattah Elsisi
//$target_screen_name = "MahaAzzam_ERC"; //Maha Azzam
//$target_screen_name = "ERC_egy";
$target_screen_name = "dr_gamalheshmat";
$next_cursor = -1;
$acc = array();
do 
{
	if ($next_cursor >= 0) 
		sleep(61); //prevent Rate limit exceeding Exception (one request per minute)
	
	try {
		$collection = $client->get('followers/ids.json', [
	        'screen_name' => "$target_screen_name",
	        'count' => '5000',
	        'stringify_ids' => 'true',
	        'cursor' => "$next_cursor"
	        ]); 
	
	$acc = array_merge($acc, $collection->ids);
	$next_cursor = $collection->next_cursor_str;
	echo "\n$next_cursor";
	} catch (HttpException $e) {
		// cURL communication successful but something went wrong with Twitter APIs.
		$message = $e->getMessage();    // Message
		$code    = $e->getCode();       // Error code (-1 if not available)
		$status  = $e->getStatusCode(); // HTTP status code
		echo "\nmessage:$message";
		echo "\ncode:$code";
		echo "\nstatus:$status";
		break;
	}
	
	sleep(61); //prevent Rate limit exceeding Exception (one request per minute)
} while( $next_cursor != 0 );

file_put_contents("list-of-followers-id-$target_screen_name.json", json_encode($acc,true));
?>