MastodonAPIに先週の日曜日に鞍替え。#脱TwitterAPI有料化

20230210

Logging

おはようございます、TwitterAPIの有料化始まりましたね😖。

企業ではどういう対応を取るのでしょうか。個人で作っていたサービスはサービス閉鎖する人達が増えてきましたね。自分もBotで高知県の企業を応援するサービスを作っていたのだけど、2月5日にサービスを停止しました。

このブログは予約投稿なので、これが配信された時にはTwitterから具体的なAPIの値段などが発表されていると思います。その発表次第ですがBotを再稼働するという選択も残っているのですが、どうなるかは分からないです。

そんな中でPHP言語を使用しMastodonのAPIを使って「投稿だけ」する。コードを書きましたのでお裾分けです。

https://qiita.com/taoka-toshiaki/items/483340a28c03a1828400

php Mastodon.php 'テスト投稿です'
<?php
require "config.php";
class Mastodon{
    const method = "POST";
    const host = "mstdn.jp";
    const endpoint  = "/api/v1/statuses";
    public static function toot($postdata = null)
    {
        if(!is_null($postdata)){
            $data = http_build_query($postdata);
            exec('curl -X POST -d "'.$data.'" --header "Authorization: Bearer '.ACCESSTOKEN.'" -sS https://'.self::host . self::endpoint.'; echo $?',$output);
            var_dump($output);
        }
    }
}
//    「未収載」    -> 'unlisted'
//    「公開」      -> 'public'
//    「非公開」    -> 'private'
//    「ダイレクト」 -> 'direct'
if($argv[1]){
    $postdata = [
        "visibility"=>"public",
        "status"=>strip_tags($argv[1]),
    ];
    Mastodon::toot($postdata);
}
<?php
define('ACCESSTOKEN','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

タグ

ACCESSTOKEN, API, argv, Authorization, Bearer, BOT, echo, endpoint, exec, gt, header, lt, mastodon, null, php define, php require, quot, toot, Twitter, TwitterAPI,