2

使用 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"
    }
  }
}

我应该怎么做才能更新视频的标题和描述,或者更好地添加一个文件并在同一个调用中设置它的标题和描述?

4

1 回答 1

-2

我建议为 YouTube v3 使用 PHP 客户端库

这是搜索视频的示例

更多示例在同一目录中。

于 2013-07-23T18:00:43.700 回答