【问题标题】:Posting image/video to Twitter将图像/视频发布到 Twitter
【发布时间】:2016-02-05 01:26:56
【问题描述】:

当我尝试使用 Twitter API 发布图片或视频时,我收到错误消息

{"request":"/i/media/upload.json","error":"段不加起来 提供总文件大小。"}

Google 没有关于此错误的任何信息。 Twitter 文档也无济于事。怎么了?

我在做什么:

1. INIT uploading file

> $this->SetUrl('upload.twitter.com/i/media/upload.json?command=INIT&media_type=image%2Fpng&total_bytes='
> . $filesize)
>                 ->SendRequest('post');

2. APPEND chunks

> $reply = ['media' => $chunk];
> $this->SetQuery($reply);
> $this->SetUrl('upload.twitter.com/i/media/upload.json?command=APPEND&media_id='.$mediaId.'&segment_index='.$segment_id)->SendRequest('post');

3. When I FINALIZE, I get the error

> $this->SetUrl('upload.twitter.com/i/media/upload.json?command=FINALIZE&media_id='
> . $mediaId)->SendRequest('post');

【问题讨论】:

  • 更正语法;改进的格式。实际的问题演示很好。
  • 您解决了吗?我遇到了同样的问题。
  • 丽莎,我的问题是我收到了错误的数据块。发送完成请求时,我生成了一个错误,即块的数量不等于总文件大小。一旦计算机将 - 将在此处发布代码

标签: php twitter file-upload


【解决方案1】:
// APPEND
    $ch = curl_init();
    $file = fopen($media, 'r');
    $chunk_id = 0;

    while (!feof($file)) {

        $append_headers = array();
        $chunk = fread($file, 1048576); // 1mb per chunk
        $boundary = $chunk_id.time();
        $params  = "--" . $boundary . "\r\n"
            . "Content-Type: video/mp4\r\n"
            . "Content-Disposition: form-data; name=\"media\"; filename=\"blob\"\r\n"
            . "\r\n"
            . $chunk . "\r\n"
            . "--" . $boundary . "--";
        $append_headers[] = 'Content-Length: ' . strlen($params);
        $append_headers[] = 'Content-Type: multipart/form-data; boundary=' . $boundary;
        curl_setopt($ch, CURLOPT_HTTPHEADER, $append_headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->curl_config['useragent']);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_dir . md5($this->UserName . $this->Password));
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_dir . md5($this->UserName . $this->Password));
        curl_setopt($ch, CURLOPT_URL, 'https://upload.twitter.com/i/media/upload.json?command=APPEND&media_id=' . $init->media_id . '&segment_index=' . $chunk_id);
        $response = curl_exec($ch);
        $chunk_id++;

    }

这就是我发布块的解决方案。效果很好。

【讨论】:

    猜你喜欢
    • 2015-09-03
    • 2013-01-15
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 2017-02-08
    • 2015-07-06
    • 2015-04-24
    • 2015-10-12
    相关资源
    最近更新 更多