KazuProg's notes

技術メモや備忘録などを自由気ままに書き連ねています

Twitter API を申請してから PHP でツイートを取得するまで

作成日: ・ 更新日:

Twitter APIの申請で英作文が必要なくなったというツイートをかなり前に見たけど、特に使い道も見つからなかったので今までスルーしてきた

でも、こんな感じで記事を書いていこうと思ったので、記事を書いたら自動でTweetしてくれるプログラムがあったら便利だなと思った。

なので、まずはTwitter APIが使えるようになるまでいろいろしてみた

Twitter API の有効化の方法は、以下のページを参考にさせていただきました。今回はこの部分については解説しないので、各自で確認してください。

2021年度版 Twitter API利用申請の例文からAPIキーの取得まで詳しく解説

無事、Twitter API を有効にできたら、今度はプログラムに入っていきます。

今回は、Twitter API を PHP から利用したかったので、以下のサイトを参考にしました。

[PHP]Twitter APIを試して少しまとめた

ここで、ライブラリの準備にcomposerというツールが必要らしいので、以下のサイトを参考にインストールした。

インストール手順(サーバ)(Raspberry PI)

composerのインストール・実行にはphpなどが必要なので、あらかじめインストールしておく。

なお、私はRaspberry Pi(Raspberry Pi OS)を今回のテスト環境としています。

$ sudo apt-get install -y php

次に、composerのダウンロード・インストールを行う

$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

[PHP]Twitter APIを試して少しまとめた にあるように、PHPのライブラリをダウンロードする

※このコマンドは、Twitter APIを利用するphpファイルがあるディレクトリで実行する。

$ composer require abraham/twitteroauth
Using version ^2.0 for abraham/twitteroauth
./composer.json has been created
Running composer update abraham/twitteroauth
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - abraham/twitteroauth[2.0.0, ..., 2.0.1] require ext-curl * -> it is missing from your system. Install or enable PHP's curl extension.
    - Root composer.json requires abraham/twitteroauth ^2.0 -> satisfiable by abraham/twitteroauth[2.0.0, 2.0.1].

どうやらcurlが必要らしいのでインストール

$ sudo apt-get install php-curl

再度実行

$ composer require abraham/twitteroauth
Using version ^2.0 for abraham/twitteroauth
./composer.json has been created
Running composer update abraham/twitteroauth
Loading composer repositories with package information
Updating dependencies
Lock file operations: 2 installs, 0 updates, 0 removals
  - Locking abraham/twitteroauth (2.0.1)
  - Locking composer/ca-bundle (1.2.9)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
  - Downloading composer/ca-bundle (1.2.9)
  - Downloading abraham/twitteroauth (2.0.1)
  - Installing composer/ca-bundle (1.2.9): Extracting archive
  - Installing abraham/twitteroauth (2.0.1): Extracting archive
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!

ライブラリをダウンロードできました!

ちなみに、ライブラリは現在のパスの ./vendor/以下にダウンロードされている

[PHP]Twitter APIを試して少しまとめた にあるサンプルプログラムを実行してみる

<?php
require('./vendor/autoload.php');

use Abraham\TwitterOAuth\TwitterOAuth;

//  各自のAPIキーを入れる
$consumer_key = 'XXXXXXXXX';
$consumer_key_sercret = 'XXXXXXXXX';
$access_token = 'XXXXXXXXX';
$access_token_secret = 'XXXXXXXXX';

$connection = new TwitterOAuth($consumer_key, $consumer_key_sercret, $access_token, $access_token_secret);

$tweets = $connection->get('search/tweets', ['q' => '駆け出しエンジニア']);

var_dump($tweets);
?>

ブラウザで開くとこのようなエラーがいくつも…

Warning: Use of undefined constant CURLOPT_CONNECTTIMEOUT - assumed 'CURLOPT_CONNECTTIMEOUT' (this will throw an Error in a future version of PHP) in /var/www/html/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 575

Warning: Use of undefined constant CURLOPT_HEADER - assumed 'CURLOPT_HEADER' (this will throw an Error in a future version of PHP) in /var/www/html/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 576

Warning: Use of undefined constant CURLOPT_RETURNTRANSFER - assumed 'CURLOPT_RETURNTRANSFER' (this will throw an Error in a future version of PHP) in /var/www/html/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 577

Warning: Use of undefined constant CURLOPT_SSL_VERIFYHOST - assumed 'CURLOPT_SSL_VERIFYHOST' (this will throw an Error in a future version of PHP) in /var/www/html/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 578

Warning: Use of undefined constant CURLOPT_SSL_VERIFYPEER - assumed 'CURLOPT_SSL_VERIFYPEER' (this will throw an Error in a future version of PHP) in /var/www/html/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 579

Warning: Use of undefined constant CURLOPT_TIMEOUT - assumed 'CURLOPT_TIMEOUT' (this will throw an Error in a future version of PHP) in /var/www/html/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 580

Warning: Use of undefined constant CURLOPT_USERAGENT - assumed 'CURLOPT_USERAGENT' (this will throw an Error in a future version of PHP) in /var/www/html/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 581

Warning: Use of undefined constant CURLOPT_CAINFO - assumed 'CURLOPT_CAINFO' (this will throw an Error in a future version of PHP) in /var/www/html/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 725

php - Twitterに投稿するためのPHPスクリプトでcurlが失敗するのはなぜですか?

php-curlを新規でインストールしてapacheなどを再起動させていない場合にこのようなエラーが起こるらしので、apacheを再起動する

$ service apache2 restart

再度実行したらできました!

これからTwitter API を利用していろいろ作っていこうと思います!