Upload video on Youtube using curl and api v3
Solution 1
Unfortunately, we don't have a specific example of YouTube API v3 uploads from PHP available yet, but my general advice is:
- Use the PHP client library instead of cURL.
- Base your code on this example written for the Drive API. Because the YouTube API v3 shares a common API infrastructure with other Google APIs, examples for doing things like uploading files should be very similar across different services.
- Take a look at the Python example for the specific metadata that needs to be set in a YouTube v3 upload.
In general, there are a lot of things incorrect with your cURL code, and I can't walk through all the steps it would take to fix it, as I think using the PHP client library is a much better option. If you are convinced you want to use cURL then I'll defer to someone else to provide specific guidance.
Solution 2
Updated version: Now with custom upload url and sending of metadata with the upload process. The entire process requires 2 requests:
-
Get a custom upload location
First, make a POST request for an upload url to:
"https://www.googleapis.com/upload/youtube/v3/videos"
You will need to send 2 headers:
"Authorization": "Bearer {YOUR_ACCESS_TOKEN}" "Content-type": "application/json"
You need to send 3 parameters:
"uploadType": "resumable" "part": "snippet, status" "key": {YOUR_API_KEY}
And you will need to send the metadata for the video in the request body:
{ "snippet": { "title": {VIDEO TITLE}, "description": {VIDEO DESCRIPTION}, "tags": [{TAGS LIST}], "categoryId": {YOUTUBE CATEGORY ID} }, "status": { "privacyStatus": {"public", "unlisted" OR "private"} } }
From this request you should get a response with a "location" field in the headers.
-
POST to custom location to send file.
For the upload you need 1 header:
"Authorization": "Bearer {YOUR_ACCESS_TOKEN}"
And send the file as your data/body.
If you read through how their client works you will see they recommend retrying if you are returned errors of code 500, 502, 503, or 504. Clearly you will want to have a wait period between retries and a max number of retries. It works in my system every time, though I am using python & urllib2 instead of cURL.
Also, because of the custom upload location this version is upload resumable capable, though I have yet to need that.
Related videos on Youtube
genna
Updated on September 17, 2022Comments
-
genna almost 2 years
I would upload a video using the Youtube API v3 with curl in PHP, as described here: https://developers.google.com/youtube/v3/docs/videos/insert
I've this function
function uploadVideo($file, $title, $description, $tags, $categoryId, $privacy) { $token = getToken(); // Tested function to retrieve the correct AuthToken $video->snippet['title'] = $title; $video->snippet['description'] = $description; $video->snippet['categoryId'] = $categoryId; $video->snippet['tags'] = $tags; // array $video->snippet['privacyStatus'] = $privacy; $res = json_encode($video); $parms = array( 'part' => 'snippet', 'file' => '@'.$_SERVER['DOCUMENT_ROOT'].'/complete/path/to/'.$file 'video' => $res ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/upload/youtube/v3/videos'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $parms); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$token['access_token'])); $return = json_decode(curl_exec($ch)); curl_close($ch); return $return; }
But it returns this
stdClass Object ( [error] => stdClass Object ( [errors] => Array ( [0] => stdClass Object ( [domain] => global [reason] => badContent [message] => Unsupported content with type: application/octet-stream ) ) [code] => 400 [message] => Unsupported content with type: application/octet-stream ) )
The file is an MP4.
Anyone can help?
-
genna over 11 yearsThanks! My attempt was to make something lighter, but I'll try the official Google library
-
genna over 11 yearsIn the Google_YoutubeService.php I can't find a function to call the insert method I need for. All methods are for listing ( channels, playlists, videos .. ) I have to wait for a newer version of the library?
-
genna over 11 yearsI've decided to use the Zend_Gdata_YouTube and the 2nd version of the API, due to the lack of the methods I need for
-
Jeff Posnick over 11 yearsYou can use the Zend client library and v2 of the API if you'd like, sure. Here's the method to use with v3 of the API, though: code.google.com/p/google-api-php-client/source/browse/trunk/src/…
-
hsafarya about 11 yearsI also had problem to set parameters with POST upload request, did you still use 2 requests to upload and update meta data ?
-
Chad Befus about 11 yearsI don't anymore. I have changed my method for this to something much more stable and I will edit my answer to the new method.
-
Supertecnoboff about 10 years@ChadBefus Are the three parameters part of the header or the body or are they part of the URL as queries?
-
Supertecnoboff about 10 years@ChadBefus I just get the response "Not found". Do you know whats wrong?
-
Supertecnoboff about 10 years@ChadBefus I'd just like tho thank you sir. I have been following this post as a guide for a video POST request to YouTube from my iOS Application and it works an absolute treat. Thank you :)
-
daamsie about 8 yearsI had to set the Content-Type to video/* to get this to work as it kicks up an error otherwise.
-
Pierre over 5 yearsThe second request uses PUT, not POST. All the information about resumable uploads are explained here: developers.google.com/youtube/v3/guides/…