使用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"
}
}
}
我应该怎么做才能更新视频的标题和描述,或者更好地添加一个文件并在同一个调用中设置其标题和描述?
我建议使用YouTube v3的PHP客户端库。
以下是搜索视频的示例。
同一目录中的更多示例。