0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

php で twitter のタイムラインを取得する

Last updated at Posted at 2017-05-12

ライブラリーのインストール

composer require vlucas/phpdotenv
get_twitter_timeline.php
# ! /usr/bin/php
<?php
// --------------------------------------------------------------------
//
//	get_twitter_timeline.php
//
//					Apr/26/2019
//
// --------------------------------------------------------------------
function get_proc($request_url,$context)
{
	$curl = curl_init();
	curl_setopt( $curl, CURLOPT_URL , $request_url );
	curl_setopt( $curl, CURLOPT_HEADER, true );
	curl_setopt( $curl, CURLOPT_CUSTOMREQUEST , $context['http']['method'] ) ;
	curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER , false );
	curl_setopt( $curl, CURLOPT_RETURNTRANSFER , true );
	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 );

	$json = substr( $res1, $res2['header_size'] );

	return	$json;
}
// --------------------------------------------------------------------
// 設定
require_once './vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

//
$consumer_key = $_ENV['CONSUMER_KEY'];
$consumer_secret = $_ENV['CONSUMER_SECRET'];
$access_token = $_ENV['ACCESS_TOKEN'];
$access_token_secret = $_ENV['ACCESS_TOKEN_SECRET'];

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

$request_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$request_method = 'GET' ;

$params_a = array(
    'screen_name' => '@ekzemplaro' ,
    'count' => 10 ,
);

// キーを作成する (URLエンコードする)
$signature_key = rawurlencode( $consumer_secret ) . '&' . rawurlencode( $access_token_secret ) ;

// パラメータB (署名の材料用)
$params_b = array(
    'oauth_token' => $access_token ,
    'oauth_consumer_key' => $consumer_key ,
    'oauth_signature_method' => 'HMAC-SHA1' ,
    'oauth_timestamp' => time() ,
    'oauth_nonce' => microtime() ,
    'oauth_version' => '1.0' ,
) ;

// パラメータAとパラメータBを合成してパラメータCを作る
$params_c = array_merge( $params_a , $params_b ) ;

// 連想配列をアルファベット順に並び替える
ksort( $params_c ) ;

// パラメータの連想配列を[キー=値&キー=値...]の文字列に変換する
$request_params = http_build_query( $params_c , '' , '&' ) ;

// 一部の文字列をフォロー
$request_params = str_replace( array( '+' , '%7E' ) , array( '%20' , '~' ) , $request_params ) ;

// 変換した文字列をURLエンコードする
$request_params = rawurlencode( $request_params ) ;

// リクエストメソッドをURLエンコードする
// ここでは、URL末尾の[?]以下は付けないこと
$encoded_request_method = rawurlencode( $request_method ) ;

// リクエストURLをURLエンコードする
$encoded_request_url = rawurlencode( $request_url ) ;

// リクエストメソッド、リクエストURL、パラメータを[&]で繋ぐ
$signature_data = $encoded_request_method . '&' . $encoded_request_url . '&' . $request_params ;

// キー[$signature_key]とデータ[$signature_data]を利用して、HMAC-SHA1方式のハッシュ値に変換する
$hash = hash_hmac( 'sha1' , $signature_data , $signature_key , TRUE ) ;

// base64エンコードして、署名[$signature]が完成する
$signature = base64_encode( $hash ) ;

// パラメータの連想配列、[$params]に、作成した署名を加える
$params_c['oauth_signature'] = $signature ;

// パラメータの連想配列を[キー=値,キー=値,...]の文字列に変換する
$header_params = http_build_query( $params_c , '' , ',' ) ;

// リクエスト用のコンテキスト
$context = array(
    'http' => array(
        'method' => $request_method , // リクエストメソッド
        'header' => array(            // ヘッダー
            'Authorization: OAuth ' . $header_params ,
        ) ,
    ) ,
) ;

// パラメータがある場合、URLの末尾に追加
if( $params_a ) {
    $request_url .= '?' . http_build_query( $params_a ) ;
}

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

echo $json;

fputs (STDERR,"*** 終了 ***\n");

// --------------------------------------------------------------------
?>
.env
CONSUMER_KEY = '******'
CONSUMER_SECRET = '******'
ACCESS_TOKEN = '******'
ACCESS_TOKEN_SECRET = '******'

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

$ uname -a
Linux iwata 5.6.13-arch1-1 #1 SMP PREEMPT Thu, 14 May 2020 06:52:53 +0000 x86_64 GNU/Linux

$ php --version
PHP 7.4.6 (cli) (built: May 12 2020 15:48:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?