Created
January 20, 2019 09:22
-
-
Save TsuyoshiTomozawa/54979a8f71dfe879272b0a1b50b2a1ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$option = getopt('k:c'); | |
$keyword = $option['k']; | |
$count = $option['c']; | |
if(!$keyword) { | |
echo '[TwitterAPI Error]search keyword is required.'; | |
die(); | |
} | |
if(!$count) { | |
echo '[TwitterAPI Error]count is required.'; | |
die(); | |
} | |
$OAUTH_CONSUMER_KEY = "***"; | |
$OAUTH_SECRET = '***'; | |
$OAUTH_TOKEN = "***"; | |
$OAUTH_TOKEN_SECRET = '***'; | |
$OAUTH_VERSION = "1.0"; | |
$OAUTH_SIGNATURE_METHOD = "HMAC-SHA1"; | |
$TWITTER_API_URL = 'https://api.twitter.com/1.1/search/tweets.json'; | |
$REQUEST_COUNT = $count; | |
$REQUEST_METHOD = 'GET' ; | |
//検索するキーワードの設定 | |
$SEARCH_KEYWORD = $keyword; | |
$oauth_signature_key = rawurlencode($OAUTH_SECRET) . '&' . rawurlencode($OAUTH_TOKEN_SECRET) ; | |
$oauth_nonce = microtime(); | |
$oauth_timestamp = time(); | |
$oauth_signature_param = 'count=' . $REQUEST_COUNT . | |
'&oauth_consumer_key=' . $OAUTH_CONSUMER_KEY . | |
'&oauth_nonce='.rawurlencode($oauth_nonce) . | |
'&oauth_signature_method='. $OAUTH_SIGNATURE_METHOD . | |
'&oauth_timestamp=' . $oauth_timestamp . | |
'&oauth_token=' . $OAUTH_TOKEN . | |
'&oauth_version=' . $OAUTH_VERSION . | |
'&q=' . rawurlencode($SEARCH_KEYWORD); | |
// データ部分の作成 | |
$oauth_signature_date = rawurlencode($REQUEST_METHOD) . '&' . rawurlencode($TWITTER_API_URL) . '&' . rawurlencode($oauth_signature_param); | |
$oauth_signature_hash = hash_hmac( 'sha1' , $oauth_signature_date , $oauth_signature_key , TRUE ) ; | |
$oauth_signature = base64_encode( $oauth_signature_hash ); | |
$req_oauth_header = array("Authorization: OAuth " . 'count=' . rawurlencode($REQUEST_COUNT) . | |
',oauth_consumer_key=' . rawurlencode($OAUTH_CONSUMER_KEY) . | |
',oauth_nonce='.str_replace(" ","+",$oauth_nonce) . | |
',oauth_signature_method='. rawurlencode($OAUTH_SIGNATURE_METHOD) . | |
',oauth_timestamp=' . rawurlencode($oauth_timestamp) . | |
',oauth_token=' . rawurlencode($OAUTH_TOKEN) . | |
',oauth_version=' . rawurlencode($OAUTH_VERSION) . | |
',q=' . rawurlencode($SEARCH_KEYWORD) . | |
',oauth_signature='.rawurlencode($oauth_signature)); | |
$TWITTER_API_URL .= '?q=' . rawurlencode($SEARCH_KEYWORD) . '&count=' . rawurlencode($REQUEST_COUNT); | |
$curl = curl_init() ; | |
curl_setopt( $curl , CURLOPT_URL , $TWITTER_API_URL ) ; | |
curl_setopt( $curl , CURLOPT_HEADER, false ) ; | |
curl_setopt( $curl , CURLOPT_CUSTOMREQUEST , $REQUEST_METHOD ) ; | |
curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false ) ; | |
curl_setopt( $curl , CURLOPT_RETURNTRANSFER , true ) ; | |
curl_setopt( $curl , CURLOPT_HTTPHEADER , $req_oauth_header ) ; | |
curl_setopt( $curl , CURLOPT_TIMEOUT , 5 ) ; | |
$res_str = curl_exec( $curl ) ; | |
curl_close( $curl ) ; | |
$res_str_arr = json_decode($res_str, ture) ; | |
foreach ($res_str_arr['statuses'] as $twit_result){ | |
$twit_content = $twit_result['text']; | |
$twit_time = date("Y-m-d H:i:s",strtotime($twit_result['created_at'])); | |
echo $twit_content." | ".$twit_time.PHP_EOL ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment