使用api v3和cUrl更新youtube视频

时间:2013-07-23 13:02:52

标签: php curl youtube-api

使用PHP cUrl更新视频时,会显示一条消息,指出无法找到视频,但视频确实存在且我可以添加视频。

代码:

    $get_data  = array(
      'part'        => 'statistics', 
      'key'         => $youtube.key,
      'id'          => $video_id,
    );

   function get_curl_response($curl, $data = NULL)
    {
      static $buffer = '';
      if(is_null($curl))
      {
        $r      = $buffer;
        $buffer = '';
        return $r;
      }
      else
      {
        $buffer .= $data;
        return strlen($data);
      }
    }

$url  = 'https://www.googleapis.com/youtube/v3/videos';
$url  .= '?' . http_build_query($get_data);

$data = array(
  'id'      => $video_id,
  'videoId' => $video_id,
  'kind'    => 'youtube#video',
  'categoryId'  => '22',
  'snippet' => array(
    'channelId'   => '<CHANNEL ID>',
    'title'       => 'Test File data',
    'description' => 'Test video data',
  ),
);

$data_string  = http_build_query($data); 
$options  = array(
  CURLOPT_RETURNTRANSFER  => TRUE,
  CURLOPT_POSTFIELDS      => $data_string,
  CURLOPT_HEADER          => FALSE,
  CURLOPT_FOLLOWLOCATION  => TRUE,
  CURLOPT_PUT             => TRUE,

  CURLOPT_WRITEFUNCTION   => 'get_curl_response',
  CURLINFO_HEADER_OUT     => TRUE,
  CURLOPT_HTTPHEADER    => array(
    'Authorization: Bearer ' . $this->_account->access_token,
    'Content-type: application/json; charset=UTF-8',
    'id: ' . $video_id,
  ),
);

回应:

object(stdClass)#634 (1) {
  ["error"]=>
  object(stdClass)#631 (3) {
    ["errors"]=>
    array(1) {
      [0]=>
      object(stdClass)#635 (5) {
        ["domain"]=>
        string(13) "youtube.video"
        ["reason"]=>
        string(13) "videoNotFound"
        ["message"]=>
        string(151) "The video that you are trying to update cannot be found. Check the value of the id field in the request body to ensure that it is correct."
        ["locationType"]=>
        string(5) "other"
        ["location"]=>
        string(7) "body.id"
      }
    }
    ["code"]=>
    int(404)
    ["message"]=>
    string(151) "The video that you are trying to update cannot be found. Check the value of the id field in the request body to ensure that it is correct."
  }
}

即使我没有发送ID,它仍会返回此消息。 最奇怪的是我能够获得同一视频的评分:

    $get_data  = array(
      'key'         => $youtube_key,
      'id'          => $video_id,
    );

   function get_curl_response($curl, $data = NULL)
    {
      static $buffer = '';
      if(is_null($curl))
      {
        $r      = $buffer;
        $buffer = '';
        return $r;
      }
      else
      {
        $buffer .= $data;
        return strlen($data);
      }
    }

$url  ='https://www.googleapis.com/youtube/v3/videos/getRating';
$url  .= '?' . http_build_query($get_data);

$options  = array(
  CURLOPT_RETURNTRANSFER  => TRUE,
  CURLOPT_HEADER          => FALSE,
  CURLOPT_VERBOSE         => TRUE,
  CURLOPT_FOLLOWLOCATION  => TRUE,
  CURLOPT_WRITEFUNCTION   => 'get_curl_response',
  CURLINFO_HEADER_OUT     => TRUE,
  CURLOPT_HTTPHEADER    => array(
    'Authorization: Bearer ' . $this->_account->access_token,
  ),
);

回复是:

object(stdClass)#634 (3) {
  ["kind"]=>
  string(30) "youtube#videoGetRatingResponse"
  ["etag"]=>
  string(57) ""<ETAG>""
  ["items"]=>
  array(1) {
    [0]=>
    object(stdClass)#630 (2) {
      ["videoId"]=>
      string(11) "<VIDEO ID>"
      ["rating"]=>
      string(4) "none"
    }
  }
}

我应该怎么做才能更新视频的标题和说明,甚至更好地添加文件并在同一个电话中设置标题和说明?

1 个答案:

答案 0 :(得分:-2)