LoginSignup
0
0

More than 5 years have passed since last update.

Twitter API で 画像投稿時にエラー

Last updated at Posted at 2018-10-13

Twitter API を使った画像投稿にtmhOAuth を使っており、以前(2013年頃)までは以下のコードで投稿出来ていました。

    $filename = 'image.jpg';
    $image_path = '/path/to/' . $filename;

    $code = $tmhOAuth->request('POST', 'https://api.twitter.com/1.1/statuses/update_with_media.json',
                array(
                    'media[]'  => "@{$image_path}", 
                    'status'   => $str,
                ),
                true, 
                true  
            );

ところが、最近実装すると、それだとエラーが返って来ました。

    // var_dump($tmhOAuth->response);
    {"errors":[{"code":189,"message":"Error creating status."}]}

頑張ってググった結果、以下のように修正すると解消し、投稿できました。

    $filename = 'image.jpg';
    $image_path = '/path/to/' . $filename;

    $handle = fopen($filename, "rb");
    $image = fread($handle, filesize($filename));
    fclose($handle);

    $media = "{$image};type=image/jpeg;filename={$filename}";

    $code = $tmhOAuth->request('POST', 'https://api.twitter.com/1.1/statuses/update_with_media.json',
            array(
                'media[]'  => "@{$media}", 
                'status'   => $str,
            ),
            true,
            true
        );

どうも、media[] に渡すパラメータがいつの間にか変わったようです。

以前は、単にファイルのパスを渡すだけで良かったけど、http headerのような形式の文字列を渡す必要があると。

0
0
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
0
0