PHPを使ってツイートをコメントを付けてRetweetする方法

PHPを使ってツイートをコメントを付けてRetweetする方法は以下の通り

Twitter APIの申請やアプリの作成は割愛:https://developer.twitter.com/en/portal/dashboard

https://twitteroauth.com/

を使用している

目次

TwitterOauthの組み込み

Retweet機能を動かすPHPファイルのフォルダで以下のコマンド

sudo composer require abraham/twitteroauth

PHPコード

<?php

//Twitter APIキー
$api_key = 'xxxxxx' ; // APIキー
$api_secret = 'yyyyyy' ; // APIシークレット
$access_token = 'zzzzzzz' ; // アクセストークン
$access_token_secret = 'aaaaaaa' ; // アクセストークンシークレット

$connection = new TwitterOAuth($api_key, $api_secret, $access_token, $access_token_secret);

//コメントとリツイートするURLを準備
$tweet = "コメントを書く";
$rt_url = "https://twitter.com/スクリーンネーム/status/ツイートID";

//引用リツイートする
$result = $connection->post("statuses/update", ["status" => $tweet, "attachment_url" => $rt_url]);

//メッセージを表示
if(isset($result->errors)) {
//失敗
echo "Error occurred.<br>Error message: " . $result->errors[0]->message;
} else {
//成功
echo "Succeeded.";
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です