LoginSignup
1
1

More than 1 year has passed since last update.

マストドンAPIでトゥートするPHPコード

Last updated at Posted at 2023-02-05

マストドンAPIでトゥートするPHPコードです。

本当はcurl_initを使用してコードを書きたかったのですが、
上手く動作しなかったので、この手法に変更しました。

php Mastodon.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);
}
config.php
<?php
define('ACCESSTOKEN','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
1
1
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
1
1