Twitter API を使って画像付きで投稿する場合、ライブラリの tmhOAuth で簡単に認証とリクエスト行うことができます。

themattharris/tmhOAuth https://github.com/themattharris/tmhOAuth

OAuth 認証

Twitter への認証の際に使う、認証鍵は Twitter Developers にて取得します。

Twitter Developers https://dev.twitter.com/

認証の際に「Problem with the SSL CA cert」と言われたので、

curlsslverifypeer を一時的に false にしました。

$twConf = array(
    'consumer_key'    => '****************************',
    'consumer_secret' => '**************************************************',
    'user_token'      => '**************************************************',
    'user_secret'     => '*********************************************',
    'curl_ssl_verifypeer' => false
);

require './tmhOAuth.php';
$tmhOAuth = new tmhOAuth($twConf);

画像を投稿する

試しに、同じ階層にある画像を読んで投稿します。

$image = "/var/www/html/sample.jpg";
$message = "ロボットからの投稿テスト";

$endpoint = $tmhOAuth->url('1.1/statuses/update_with_media');
$imageName  = basename($image);
$params = array(
    'media[]'  => "@{$image};type=image/jpeg;filename={$imageName}",
    'status'   => "{$message}"
);
$code = $tmhOAuth->request('POST', $endpoint, $params, true, true);
if ($tmhOAuth->response["code"] == 200){ // $codeにもステータスは返ってきます
    var_dump($tmhOAuth->response["response"]);
} else {
    var_dump($tmhOAuth->response["error"]);
}

ツイートが投稿できました。