2
3

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 5 years have passed since last update.

bitflyerのAPIを使ってTwitterに板情報に投稿する

2
Posted at

久しぶりの投稿はAPIを叩いてその結果をツイートする
という方法を忘れないために書いておく

環境

サンプル

 
$settings = array(
    'oauth_access_token' => "xxxxxxxxxxxxxxxxxx",
    'oauth_access_token_secret' => "ooooooooooo",
    'consumer_key' => "xxxxxxxxxxxxxxxxxxxxxxxx",
    'consumer_secret' => "ooooooooooooooooooooo"
);
 
// OAuthライブラリの読み込み
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
 
//認証情報4つ
$consumerKey = $settings['consumer_key'];
$consumerSecret = $settings['consumer_secret'];
$accessToken = $settings['oauth_access_token'];
$accessTokenSecret = $settings['oauth_access_token_secret'];
 
$base_url = 'http://api.bitflyer.jp';
 
$tag = 'PHP';
$query = ['product_code'=>'FX_BTC_JPY'];
 
$response = file_get_contents(
                  $base_url.'/v1/getboard?' .
                  http_build_query($query)
            );
 
// 結果はjson形式で返されるので
$result = json_decode($response,true);
 
$mid_price = $result["mid_price"];
 
$message = "現在のbitflyerのFX_BTC_JPYは " . $mid_price . "円 です。";

//接続
$connection = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
 
//ツイート
$res = $connection->post("statuses/update", array("status" => $message));
 
//レスポンス確認
var_dump($res);

今回、速さを優先してPHPでの取得にしてしまいました。
単純に価格を取得するだけなので、
おそらくAPIの回数規制にも引っかからないはず。

次はRubyで書いてみようかな。

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?