通过Twitter API发布媒体

时间:2018-03-08 14:01:54

标签: laravel api twitter

我正在使用Twitter API将媒体发布到我的Twitter帐户。就我而言,我通过每分钟运行的cron任务来创建它(从数据库获取数据并在public function handle() { Log::useDailyFiles(storage_path() . '/logs/post_schedule.log'); $posts = Preview::getUnprocessed(); foreach ($posts as $key => $post) { $date_to = Carbon::parse($post->date_send); if (("Twitter" === ucfirst($post->social)) && ($date_to->lt(Carbon::now()))) { Log::info('***************** start sending*****************'); dump('sending'); try { $this->postTwitter($post); } catch (\Exception $e) { dump($e->getMessage()); } Log::info('***************** post:' . $post->id . ' at date : ' . $date_to . ' *****************'); $post->state = true; $post->save(); Log::info('***************** finsih sending *****************'); } } } 时发送)。

正确发布文字和图片但我尝试发布视频时遇到问题。

这是我到目前为止所拥有的:

控制台命令:

public static function postTwitter($post)
{

    $tweet = ['tweet' => $post->content];
    $images = $post->uploads;
    $newTwitte = ['status' => $tweet];

    if (!empty($images)) {
        foreach ($images as $key => $value) {
            $uploaded_media = Twitter::uploadMedia(['media' => File::get(public_path($value->filename))], true);
            if (!empty($uploaded_media)) {
                $newTwitte['media_ids'][$uploaded_media->media_id_string] = $uploaded_media->media_id_string;
            }
        }
    }

    $twitter = Twitter::postTweet($newTwitte);
}

Twitter发布功能:

d.DateofVisit >= DATEADD(wk, DATEDIFF(wk, 0, GETDATE()) - 8, 0)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用包Abraham \ TwitterOAuth \ TwitterOAuth后,  我在功能上做了一些改变。 Twitter发布功能:

public function postTwitter($post)
    {
        $images = $post->uploads;

        $ids = [];   
        foreach ($images as $key => $value) 
        {
            $file = public_path($value->filename);
            $extension = File::extension($file);

            if ($extension == "mp4") 
            {

                $data['media']      = $file;
                $data['media_type'] = 'video/mp4';
                $code  = $this->connection->upload('media/upload', $data, true);
                $parameters = [
                    'status' => $post->content,
                    'media_ids' => $code->media_id_string,
                ];
                $result = $this->connection->post('statuses/update', $parameters);
            }
            else
            {     
                $code  = $this->connection->upload('media/upload',['media' => $file], false);

            }


            $ids[] = $code->media_id_string;

        }

        $parameters = [
            'status' => $post->content,
            'media_ids' => implode(',',$ids),
        ];

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

    }

这个功能非常好,你必须配置你的:

<强> TWITTER_CONSUMER_KEY

<强> TWITTER_CONSUMER_SECRET

<强> TWITTER_ACCESS_TOKEN

<强> TWITTER_ACCESS_TOKEN_SECRET