Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

【最終手段】PHPでTwitterOauthを使ってTwitterに画像のアップロードをする方法

こんにちは、
私は、LaravelとVueでWebアプリを作成していますが
TwitterOauthを使用して画像をアップロードするというところで
なぜか進まなくなったんですよね・・・笑

注意

今回の解決方法は非常に強引で
思わぬ所でエラーが起きる場合があるので
予めご了承ください。

出ていたエラー

exception: “InvalidArgumentException”
file:”TwitterOAuth.php”
line: 297
message: “You must supply a readable file

このようなエラーが出ていました。

Twitterへ画像のアップロードで試したこと。

 

JavaScriptで画像をBase64形式に変換をして
PHPで投稿処理をしていました。

 $media = $connection->upload('media/upload',['media' =>$dataurl]);
 $parameters = [
    'status' => $share_text,
     'media_ids' =>$media->media_id_string
    ];

$connection->post('statuses/update', $parameters);

JavaScriptから受け取ったデータは$dateurlに代入して
ツイートの処理に入れたのですが、どうしても
上に書いたようなエラーが出るのです。

で、何がいけないのか
分からなかったので、

プロジェクトフォルダに画像を入れて

$media = $connection->upload('media/upload',['media' =>'sample.png']);

ファイル名を指定して実行

してみると
画像付きでツイートできるのです。

しかし、

sample.pngをBase64方式でエンコードして
ツイートの処理を行うと同じエラーが起きるのです。

TwitterOauthのインストールをミスったのか!?
様々なページを参考にして試行錯誤しましたが、

どうしてもできない・・・。

実は、以前TwitterOauthを利用して
Base64方式にエンコードされた画像のアップロードは出来ていたので
今回も出来るかと思っていたのですがね・・。

 

解決方法は、エラーの出る処理を消す。

宜しくないのは分かります。
ですが、エラー出る処理を消したら
正常に画像のアップロードができるようになったのです。

(きちんと勉強して理解せねば・・・)

※上に記したエラーと同じ方のみ実行してください。

※コメントアウトしてコードを残すなど戻せる状態にしましょう。

修正前コード↓

 

 //TwitterOauth.php 297行付近
 private function uploadMediaNotChunked($path, array $parameters)
    {
        if (! is_readable($parameters['media']) ||
            ($file = file_get_contents($parameters['media'])) === false) {
            throw new \InvalidArgumentException('You must supply a readable file');
        }
        $parameters['media'] = base64_encode($file);
        return $this->http('POST', self::UPLOAD_HOST, $path, $parameters, false);
    }

 

修正後コード↓

  //TwitterOauth.php 297行付近 
 private function uploadMediaNotChunked($path, array $parameters)
    {
        //if (! is_readable($parameters['media']) ||
         //   ($file = file_get_contents($parameters['media'])) === false) {
        //    throw new \InvalidArgumentException('You must supply a readable file');
        //}
        $file = file_get_contents($parameters['media']);
        $parameters['media'] = base64_encode($file);
        return $this->http('POST', self::UPLOAD_HOST, $path, $parameters, false);
    }

これで一度テストしてみてください。
私は、これで直りました。

おわりに

あまりにネットの情報が少ないため
このような方法を使用してしまいました。

ドキュメント読むようにしなくては!

最後まで読んでいただき
ありがとうございました。

[amazonjs asin=”B06XFPYZ8P” locale=”JP” tmpl=”Small” title=”アイデア大全”]

コメント

スポンサーリンク
スポンサーリンク
タイトルとURLをコピーしました