2017-04-17 52 views
1

我正在創建一個小型網絡應用,允許用戶在YouTube之外更新他們的視頻。目前我測試了我自己,我跑入PHP - 404未找到視頻(視頻:PUT)YouTube API

{ "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。

我使用捲曲發送PUT請求PHP如下:

$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

使用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); 
?> 
+0

驚人!你知道爲什麼它不適合我嗎? – ConorReidd