使用PHP以异步方式将视频发布到Twitter

时间:2016-08-09 17:02:56

标签: php asynchronous twitter

我目前正在使用Codebird PHP(https://github.com/jublonet/codebird-php)将短视频发布到Twitter。但是,除非您发送异步https://dev.twitter.com/rest/media/uploading-media#videorecs),否则Twitter会将视频发布限制为30秒。如何使用PHP异步发布视频到Twitter?

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

Codebird::setConsumerKey(TW_CONSUMER_KEY, TW_CONSUMER_SECRET);
    $cb = \Codebird\Codebird::getInstance();
    $cb->setToken(TW_ACCESS_TOKEN, TW_ACCESS_SECRET);

    $file = OUTPUT_FOLDER . "videos/$session_id/full-video.mp4";
    $size_bytes = filesize($file);
    $fp         = fopen($file, 'r');

    // INIT the upload
    $reply = $cb->media_upload([
        'command'     => 'INIT',
        'media_type'  => 'video/mp4',
        'total_bytes' => $size_bytes
    ]);

    $media_id = $reply->media_id_string;

    // APPEND data to the upload
    $segment_id = 0;

    while (! feof($fp)) {
        echo "appending... $file";
        $chunk = fread($fp, 1048576); // 1MB per chunk for this sample

        $reply = $cb->media_upload([
            'command'       => 'APPEND',
            'media_id'      => $media_id,
            'segment_index' => $segment_id,
            'media'         => $chunk
        ]);

        $segment_id++;
    }

    fclose($fp);

    // FINALIZE the upload
    $reply = $cb->media_upload([
        'command'       => 'FINALIZE',
        'media_id'      => $media_id
    ]);

    var_dump($reply);

    if ($reply->httpstatus < 200 || $reply->httpstatus > 299) {
        die();
    }

    // Now use the media_id in a Tweet
    $reply = $cb->statuses_update([
        'status'    => "$handle",
        'media_ids' => $media_id
    ]);
    print_r($reply);
    /*
     * 
     */
    //$this->curl_post_async("statuses/update")
    $success = false;
    if ($reply) {
        $success = true;
    }

    return $success;

0 个答案:

没有答案