PHP - 404未找到视频(视频:PUT)YouTube API

时间:2017-04-17 12:25:38

标签: php api curl youtube youtube-api

我正在创建一个小型网络应用,允许用户在YouTube之外更新自己的视频。我正在测试自己,我正在进入

{ "error": { "errors": [ { "domain": "youtube.video", "reason": "videoNotFound", "message": "The video that you are trying to update cannot be found. Check the value of the \u003ccode\u003eid\u003c/code\u003e field in the request body to ensure that it is correct.", "locationType": "other", "location": "body.id" } ], "code": 404, "message": "The video that you are trying to update cannot be found. Check the value of the \u003ccode\u003eid\u003c/code\u003e field in the request body to ensure that it is correct." } }

我确定知道的事情:

  • 我有正确的授权码
  • 我有这个授权码引用的正确频道。
  • 我有正确的视频ID。

我在PHP中使用cURL发送PUT请求,如下所示:

$curl = curl_init($url . "https://www.googleapis.com/youtube/v3/videos?part=snippet&access_token=".$token)

$data = array(
'kind' => 'youtube#video',
'id' => 'theCorrectIdIsHere',
'snippet' => array(
    "title" => "Test title done through php",
    "categoryId" => "1"
),
);`

那么当我使用以下方式执行此操作时:

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: 
application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));`

有人问过类似的问题:Update title and description using YouTube v3 API?但是,答案是他下载并更换视频,这需要额外的配额而且不应该完成

注意此处完成的API测试一切正常:https://developers.google.com/youtube/v3/docs/videos/update

1 个答案:

答案 0 :(得分:1)

使用json_encode设置请求数据&使用Authorization标头设置访问令牌:

<?php

$curl = curl_init();

$access_token = "YOUR_ACCESS_TOKEN";

$data = array(
'kind' => 'youtube#video',
'id' => 'YOUR_VIDEO_ID',
'snippet' => array(
    "title" => "Test title done through php",
    "categoryId" => "1"
)
);
curl_setopt($curl,CURLOPT_URL, "https://www.googleapis.com/youtube/v3/videos?part=snippet");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Bearer ' . $access_token));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_VERBOSE, true);

$result = curl_exec($curl);

curl_close($curl);

var_dump($result);
?>