上传 Twitter 视频错误 (PHP).API 响应:段加起来没有达到提供的总文件大小 [英] Upload Twitter video error (PHP). API response: Segments do not add up to provided total file size

查看:43
本文介绍了上传 Twitter 视频错误 (PHP).API 响应:段加起来没有达到提供的总文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 API 和 tmhOAuth 将视频上传到 Twitter 以进行请求,但是我收到来自 Twitter 的错误消息:分段加起来没有达到提供的总文件大小.".

I'm trying to upload video to Twitter using API and tmhOAuth for requests however I'm getting an error from Twitter: "Segments do not add up to provided total file size.".

我用 INIT 命令中提供的大小检查了块大小,它们是相等的.你能帮我找到解决方案吗?

I checked chunks sizes with size provided in INIT command they are equal. Could you please help me find solution?

这是我用作基础的解决方案

这是我的代码:

/**
 * @param string $pathToVideo
 *
 * @return string
 *
 * @throws UploadException
 * @throws \RuntimeException
 */
public function uploadVideo($pathToVideo)
{
    //execute upload init and get media ID form init command
    $mediaId = $this->init($pathToVideo);

    //uploading video
    $this->append($pathToVideo, $mediaId);

    //finalize uploading
    $code = $this->finalize($mediaId);
}


/**
 * @param string $mediaId
 *
 * @return int
 */
private function finalize($mediaId)
{
    return $this->sendPostRequest(['command'  => 'FINALIZE', 'media_id' => $mediaId]);
}

/**
 * @param string $pathToVideo
 * @param string $mediaId
 *
 * @throws UploadException
 */
private function append($pathToVideo, $mediaId)
{
    //read video file
    $fp = fopen($pathToVideo, 'r');
    //segment counter
    $segmentId = 0;
    $uploadedBytes = 0;

    while (! feof($fp)) {
        $chunk = fread($fp, self::CHUNK_LIMIT);
        $uploadedBytes += strlen($chunk);
        $code = $this->sendPostRequest([
            'command'       => 'APPEND',
            'media_id'      => $mediaId,
            'media'         => $chunk,
            'segment_index' => $segmentId,
        ], true);

        if (!in_array($code, [Response::HTTP_CONTINUE, Response::HTTP_NO_CONTENT], true)) {
            throw new UploadException(sprintf(
                "Uploading Twitter video failed during APPEND. Returned code %d.\nPath to video %s.\nResponse: %s",
                $code,
                $pathToVideo,
                $this->tmhOAuth->response['response']
            ));
        }

        $segmentId++;
    }

    fclose($fp);
}

/**
 * @param string $pathToVideo
 *
 * @return string
 *
 * @throws UploadException
 */
private function init($pathToVideo)
{
    $fileSize = filesize($pathToVideo);
    $code = $this->sendPostRequest([
        'command'     => 'INIT',
        'total_bytes' => $fileSize,
        'media_type'  => 'video/mp4',
    ]);

    $response = $this->tmhOAuth->response['response'];

    if ($this->isSuccessCode($code)) {
        //return media ID form init command
        return json_decode($response)->media_id_string;
    }

    throw new UploadException(sprintf(
        "Uploading Twitter video failed during INIT. Returned code %d.
        Path to video %s, file size %d.\nEndpoint: " . $this->uploadUrl . "\nResponse %s",
        $code,
        $pathToVideo,
        $fileSize,
        $response
    ));
}

/**
 * @param array $options
 *
 * @return int
 */
private function sendPostRequest($options, $isMultipart = false)
{
    return $this->tmhOAuth->request(
        'POST',
        $this->uploadUrl,
        $options,
        true,
        $isMultipart
    );
}

推荐答案

我找到了解决方案.当我将 CHUNK_LIMIT 设置为 25000 时,它开始工作.我尝试使用 50000 但最终失败了.

I found solution. When I set CHUNK_LIMIT to 25000 it start working. I tried to use 50000 but it was fail at the end.

这篇关于上传 Twitter 视频错误 (PHP).API 响应:段加起来没有达到提供的总文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆