【问题标题】:how to tweet video using codebird API如何使用 Codebird API 发布视频
【发布时间】:2017-04-23 16:03:45
【问题描述】:

我正在使用 codebird 上传视频。

状态文本显示在 twitter 帐户中,但未上传/显示视频。

这是推特回复。

1470928 字节 object(stdClass)#7 (3) { ["errors"]=> array(1) { [0]=> object(stdClass)#6 (2) { ["code"]=> int(324) [" message"]=> string(35) "无效的媒体 ID 806853054318448640" } } ["httpstatus"]=> int(400) ["rate"]=> NULL }

和代码

    require_once ('codebird/src/codebird.php');
\Codebird\Codebird::setConsumerKey('xyz','XXXXXXX'); // static, see README

$cb = \Codebird\Codebird::getInstance();

$cb->setToken('Xyz', 'xyz');
  $file       = 'www.something.com/*.mp4';
  $size_bytes = filesize($file);

$ch = curl_init($file);
  curl_setopt($ch, CURLOPT_NOBODY, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

  $data = curl_exec($ch);
  curl_close($ch);
  echo '<br>';
  if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {

      // Contains file size in bytes
      $contentLength = (int)$matches[1];

  }

echo  $size_bytes = $contentLength ;

$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;
 $media_id ;

$segment_id = 0;

while (! feof($fp)) {
  $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);

$reply1 = $cb->media_upload([
  'command'       => 'FINALIZE',
  'media_id'      => $media_id
]);


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

$reply2 = $cb->statuses_update([
  'status'    => 'Tw gvifdeggeo uploadsrrs.',
  'media_ids' => $media_id
]);

var_dump($reply2);

Curl 和 allow_url_fopen 已启用 帮我看看我错过了什么?或者这个回复表明了什么?

【问题讨论】:

  • 为什么sn-p代码以$media_id = $reply-&gt;media_id_string;开头?请发布整个相关代码(在这种情况下,您如何获得$reply 是高度相关的

标签: php video twitter


【解决方案1】:

获取media_id后,您已成功将媒体上传到您的帐户。下一步是使用该媒体发布推文。这是一个 sn-p 示例:

//upload the file to your twitter account
$mediaID = $reply->media_id_string;

//build the data needed to send to twitter, including the tweet and the image id
$params = array(
  'status' => $message,
  'media_ids' => $mediaID   // BE SURE TO TWITTER's NOT YOURS!
);
//post the tweet with codebird
$reply2 = $cb->statuses_update($params);

转出第二条回复

var_dump($reply2);

This snippet is taken from Stack Overflow with answer explaining how to tweet using an image as the media.

【讨论】:

  • 请移动您的var_dump - 让我们在发布推文后查看第二个 $reply
  • 啊,我看到了问题 - 你正在使用 your $media_id 并且应该使用 Twitter $reply-&gt;media_id_string; statuses_update() 需要一个名为 media_ids 的参数,并带有 s。您也必须更新它,因为您发送的 media_id 没有 s
  • object(stdClass)#4 (25) { ["created_at"]=> string(30) "Thu Dec 08 12:30:12 +0000 2016" ["id"]=> int (806838285167427584) ["id_str"]=> string(18) "806838285167427584" ["text"]=> string(20) "Tw video uploadsrrs." ["截断"]=> bool(false) ["entities"]=> object(stdClass)#5 (4) { ["hashtags"]=> array(0) { } ["symbols"]=> array( 0) { } ["user_mentions"]=> array(0) { } ["urls"]=> array(0) { } } ["source"]=> string(60) "dadlapi.com" ["in_reply_to_status_id "]=> NULL ["in_reply_to_status_id_str"]=> NULL ["in_reply_to_user_id"]=> NULL ["in_reply_to_user_id_str"]=> 在此配置文件信息之后为 NULL..
  • 您是否将参数更新为media_ids。请更新您的帖子,不要将更新放在 cmets 中。
  • 更新示例中的代码。注意:我仍然不明白为什么你的代码以一行开头,表明你已经收到了来自 Twitter 的回复。
猜你喜欢
  • 1970-01-01
  • 2019-08-02
  • 2023-03-04
  • 1970-01-01
  • 2019-04-01
  • 2013-12-08
  • 2013-06-18
  • 2015-12-29
  • 1970-01-01
相关资源
最近更新 更多