Viewed   164 times

I would upload a video using the Youtube API v3 with curl in PHP, as described here:

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, '');
    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));

    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?



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.

Thursday, August 11, 2022

This was mine implementation:

$data = '<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<entry xmlns="" xmlns:media="" xmlns:yt="">
        <media:title type="plain">' . $title . '</media:title>
        <media:description type="plain">' . trim($description) . '</media:description>
        <media:keywords>' . $tags . '</media:keywords>
        <media:category scheme="">Music</media:category>
$headers = array(
    'Authorization: GoogleLogin auth=' . $auth,
    'GData-Version: 2',
    'X-GData-Key: key=<KEY>',
    'Slug: ' . basename($videoPath)

$tmpfname = tempnam("/tmp", "youtube");
$handle = fopen($tmpfname, "w");
fwrite($handle, $data);

$post = array(
    'xml' => '@' . $tmpfname . ";type=application/atom+xml",
    'video' => '@' . $videoPath . ";type=video/mp4",
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
Thursday, August 18, 2022

Strangely a very small tweak in code fixes the problem.

Instead of just $videoPath = "/path/to/file.mp4"; using DOCUMENT_ROOT solves the issue. Other than that code of documentation is perfect.

$videoPath = $_SERVER["DOCUMENT_ROOT"] . "/mapapp/videos/test.mp4";
Saturday, December 17, 2022

The answer provided by @Ibrahim was almost correct for me. What I needed to do was edit my API configuration. However, it was not the "Simple API access" section I needed to edit, it was the settings after clicking the button "Create another client Id".

Then I could select "Installed application" -> "Android". After inputting my package name and SHA1, and waiting 15 minutes, my app worked as expect. I also have the "Simple API access" set up. I am not sure if you need both or not.

Friday, September 16, 2022

Here is the official documentation of the Resumable Upload Protocol, which is used by all of Google's public (open source) libraries.

I personally would not recommend you to implement resumable video uploading using bare HTTP request methods. That's quite tricky to done it right. But, if you don't want to use Google's libraries, you have to absorb this doc and implement its specifications the way you need it.

There's also the possibility to upload videos on one go (thus not using the above mentioned protocol). That would entail calling a POST method on the URL:,status,

where you'll have to compose a multipart/related content-type as exemplified below:

POST /upload/youtube/v3/videos?uploadType=multipart&part=snippet,status HTTP/1.1
Content-length: 453
Content-type: multipart/related; boundary="===============8268018375852491166=="
Authorization: Bearer [YOUR_VALID_ACCESS_TOKEN]

Content-Type: application/json
MIME-Version: 1.0

  "snippet": {
    "title": "test video",
    "description": "just some test video",
    "categoryId": "22"
  "status": {
    "privacyStatus": "private",
    "embeddable": false,
    "license": "youtube"

Content-Type: video/mp4
MIME-Version: 1.0
Content-Transfer-Encoding: binary



You could use as spring of inspiration for your own code this Python3 script I implemented a while ago. The script takes as input an JSON object specifying the metadata of the video to be uploaded and the video file itself and generates the multipart/related file and the associated Content-Type HTTP header that could well be used by an immendiate curl command. (Issue my script with --help for brief helping info.)

Note that my script is based on Google's own open source code, specifically on This latter script is part of Google's APIs Client Library for Python.

Wednesday, August 3, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :