CakePHP3 ライブラリ「TwitterOAuth」で自分のTwitterアカウントにダイレクトメッセージ(DM)を送信する。
◯ ライブラリ「TwitterOAuth」インストール
1 2 3 4 5 6 7 8 9 10 11 12 | # composer require abraham/twitteroauth or # php composer.phar require abraham/twitteroauth Using version ^0.7.4 for abraham/twitteroauth ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing abraham/twitteroauth (0.7.4): Downloading (100%) Writing lock file Generating autoload files > Cake\Composer\Installer\PluginInstaller::postAutoloadDump |
◯ インストール後のcomposer.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # cat composer.json { "name": "cakephp/app", "description": "CakePHP skeleton app", "homepage": "https://cakephp.org", "type": "project", "license": "MIT", "require": { "php": ">=5.6", "cakephp/cakephp": "3.5.*", "mobiledetect/mobiledetectlib": "2.*", "cakephp/migrations": "~1.0", "cakephp/plugin-installer": "~1.0", "josegonzalez/dotenv": "2.*", "abraham/twitteroauth": "^0.7.4" }, } |
◯ Twitter ダイレクトメッセージ(DM)を送信する
※ Twitter APIキー申請
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php use Abraham\TwitterOAuth\TwitterOAuth; public function index() { // Twitterコネクション生成 $ConsumerKey = "コンシューマーキー"; $ConsumerSecret = "コンシューマーシークレット"; $AccessToken = "アクセストークン"; $AccessTokenSecret = "アクセストークンシークレット"; $ScreenName = "@"."スクリーンネーム"; $PostMessages = "errorだよ!"; $connection = new TwitterOAuth($ConsumerKey, $ConsumerSecret, $AccessToken, $AccessTokenSecret); $connection->post('direct_messages/new', ['screen_name' => $ScreenName, 'text' => $PostMessages]); } |