Twitter で Bearer Token を使って フォローワーを取得 (PHP)

Consumer Key (API Key) と、Consumer Secret (API Secret) を使って、Bearer Token を取得したあと、API を使って情報を取得します。

Bearer Token の取得方法はこちら
[Twitter の Bearer Token を取得 (Node.js)](https://qiita.com/ekzemplaro/items/e86dfdc11264ded69a5a)

```php:get_followers.php
#! /usr/bin/php
<?php
// ------------------------------------------------------------------
//
//	get_followers.php
//
//						Nov/18/2017
//
// ------------------------------------------------------------------
function get_proc ($request_url,$context)
{
	$curl = curl_init() ;
	curl_setopt( $curl , CURLOPT_URL , $request_url ) ;
	curl_setopt( $curl , CURLOPT_HEADER, 1 ) ; 
	curl_setopt( $curl , CURLOPT_CUSTOMREQUEST , $context['http']['method'] ) ;			// メソッド
	curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false ) ;								// 証明書の検証を行わない
	curl_setopt( $curl , CURLOPT_RETURNTRANSFER , true ) ;								// curl_execの結果を文字列で返す
	curl_setopt( $curl , CURLOPT_HTTPHEADER , $context['http']['header'] ) ;			// ヘッダー
	curl_setopt( $curl , CURLOPT_TIMEOUT , 5 ) ;										// タイムアウトの秒数
	$res1 = curl_exec( $curl ) ;
	$res2 = curl_getinfo( $curl ) ;
	curl_close( $curl ) ;

	$str_json = substr( $res1, $res2['header_size'] ) ;
//	echo $str_json . "\n";

	return	$str_json;
}

// ------------------------------------------------------------------
function get_info_proc($bearer_token,$id)
{
	$request_url = 'https://api.twitter.com/1.1/users/show.json' ;

	$params = array(
		 "user_id" => $id
		);

	if( $params ) {
		$request_url .= '?' . http_build_query( $params ) ;
		}

	$context = array(
		'http' => array(
			'method' => 'GET' ,
			'header' => array(
				'Authorization: Bearer ' . $bearer_token ,
			) ,
		) ,
	) ;

	$str_json = get_proc ($request_url,$context);
	
	$unit_aa = json_decode ($str_json,true);
	echo $id . "\t";
	echo $unit_aa['screen_name'] . "\t";
	echo $unit_aa['name'] . "\n";
}

// ------------------------------------------------------------------
fputs (STDERR,"*** 開始 ***\n");

$bearer_token = 'AAAAAAAAAAAAAAAAAAAAAK6jTBBBBBAARpSjVBjJ5Yr%2xxxxxGQaL6EOKZto%3Dnnd6KvwMtQlxxxxx1hY7IRjCT76Yxzqjxxxxx3GqxxxxxSBNUI' ;
$request_url = 'https://api.twitter.com/1.1/followers/ids.json' ;

$params = array(
		"screen_name" => "ekzemplaro",
		"stringify_ids" => "true",
	//	"count" => "5",
	);

if( $params ) {
		$request_url .= '?' . http_build_query( $params ) ;
	}

	// リクエスト用のコンテキスト
$context = array(
		'http' => array(
			'method' => 'GET' ,
			'header' => array(
				'Authorization: Bearer ' . $bearer_token ,
			) ,
		) ,
	) ;


$str_json = get_proc ($request_url,$context);

$dict_aa = json_decode ($str_json,true);
echo "next_cursor " . $dict_aa["next_cursor"] . "\n";

$count = count($dict_aa["ids"]);
echo "count = " . $count . "\n";

for ($it = 0; $it < $count; $it++)
	{
	$id =  $dict_aa["ids"][$it];
	get_info_proc($bearer_token,$id);
	}
//
fputs (STDERR,"*** 終了 ***\n");

// ------------------------------------------------------------------
?>
```

次の環境で動作を確認しました。

```text
$ uname -a
Linux iwata 4.19.4-arch1-1-ARCH #1 SMP PREEMPT Fri Nov 23 09:06:58 UTC 2018 x86_64 GNU/Linux
$ php --version
PHP 7.2.12 (cli) (built: Nov  6 2018 15:07:37) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
```