我正在使用Twitter将媒体发布到我的Twitter帐户。在我的例子中,我通过每分钟运行的cron任务(从DB获取数据并在date == now时发送数据)。
文字和图片是正确张贴,但我有一个问题,当我试图张贴一个视频。
到目前为止,我的情况如下:
控制台命令:
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 *****************');
}
}
} Twitter发布功能:
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);
}我怎么才能解决这个问题?
发布于 2018-03-13 10:18:24
在使用了亚伯拉罕\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
https://stackoverflow.com/questions/49174956
复制相似问题