Asked  7 Months ago    Answers:  5   Viewed   41 times

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?

 Answers

12

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.

Wednesday, March 31, 2021
 
Extrakun
answered 7 Months ago
92

Did you read http://php.net/manual/en/features.file-upload.put-method.php ? Script PUT /put.php all set up?

Also, what is $image -- it needs to be a file handler, not a file name.

Ps. Using file_get_contents will try to load whatever is PUT on the server into memory. Not a good idea. See the linked manual page.

Wednesday, March 31, 2021
 
Hat
answered 7 Months ago
Hat
48

The $_FILES array being populated on PUT requests. That's because a PUT request will specify the file name in the url and the file content in the body. Nothing more.

You'll have to use php://input as you already suggested.

upload.php

<?php
/* PUT data goes to php://input */
echo file_get_contents("php://input");

Then use the following curl command line:

curl --upload -H "X-TOKEN: test123" a.txt  http://localhost/upload.php

Update after comments: Using the --upload option should fit your needs. In difference to -X PUT together with -F, the data will be send raw and get not multipart/form-data encoded.. One of the hidden treasures of curl ;)

Friday, May 28, 2021
 
steros
answered 5 Months ago
92

Just look into the following links and you will get an idea about uploading an video in you tube.

android youtube upload video with static username and password

YouTube API 3 Upload Video - Access not configured - Android

Also here i will provide you a demo project link as same as you required.

https://github.com/youtube/yt-direct-lite-android

just go through it and hope it will works.

Friday, September 24, 2021
 
arsena
answered 1 Month ago
46

You will have to make a call to the Youtube Data API's Video resource after you make the search call. You can put up to 50 video id's in search, so you wont have to call it for each element.

https://developers.google.com/youtube/v3/docs/videos/list

You'll want to set part=contentDetails, because duration is there.

For example the following call:

https://www.googleapis.com/youtube/v3/videos?id=9bZkp7q19f0&part=contentDetails&key={YOUR_API_KEY} Gives this result:

{
 "kind": "youtube#videoListResponse",
 "etag": ""XlbeM5oNbUofJuiuGi6IkumnZR8/ny1S4th-ku477VARrY_U4tIqcTw"",
 "items": [
  {

   "id": "9bZkp7q19f0",
   "kind": "youtube#video",
   "etag": ""XlbeM5oNbUofJuiuGi6IkumnZR8/HN8ILnw-DBXyCcTsc7JG0z51BGg"",
   "contentDetails": {
    "duration": "PT4M13S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": true,
    "regionRestriction": {
     "blocked": [
      "DE"
     ]
    }
   }
  }
 ]
}
Saturday, October 16, 2021
 
Gzp
answered 1 Week ago
Gzp
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :