0
votes

I'm trying to upload videos to youtube using the Google API and am getting this error Notice: Undefined variable: htmlBody in /home/critter/public_html/test-video.php on line 136

A service error occurred: Error calling PUT https://www.googleapis.com/upload/youtube/v3/videos?part=status%2Csnippet&uploadType=resumable&upload_id=AEnB2Uq0uH7d-5-9CbV9zld6jxA867c4phTe_0GYqJ5MOksKdRaD-u_VDT22bC-CQAm-_TllLykBr3Gf1OL9WDnnoPiyw8Em5Q: (400) Invalid Upload Request

I have verified that the file I'm trying to upload is being correctly opened.

The script is failing in the upload while loop. I've spent hours searching for a solution and so far no luck. Can anyone help?

<?php
$path_to_add = 'mypath/google-api-php-client/src';
// Call set_include_path() as needed to point to your client library.
set_include_path(get_include_path() . ':/' . $path_to_add);
require_once 'Google_Client.php';
require_once 'contrib/Google_YouTubeService.php';
session_start();

/* You can acquire an OAuth 2 ID/secret pair from the API Access tab on the Google APIs Console
 <http://code.google.com/apis/console#access>
For more information about using OAuth2 to access Google APIs, please visit:
<https://developers.google.com/accounts/docs/OAuth2>
Please ensure that you have enabled the YouTube Data API for your project. */
$OAUTH2_CLIENT_ID = 'id';
$OAUTH2_CLIENT_SECRET = 'secret';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
    FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// YouTube object used to make all API requests.
$youtube = new Google_YoutubeService($client);

if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

// Check if access token successfully acquired
if ($client->getAccessToken()) {
  try{
    // REPLACE with the path to your file that you want to upload
    $videoPath = "/path/to/myfile.mp4";

    // Create a snipet with title, description, tags and category id
    $snippet = new Google_VideoSnippet();
    $snippet->setTitle("Pets & Animals");
    $snippet->setDescription("My Family");
    $snippet->setTags(array("dog", "smart"));

    // Numeric video category. See
    // https://developers.google.com/youtube/v3/docs/videoCategories/list 
    $snippet->setCategoryId("15");

    // Create a video status with privacy status. Options are "public", "private" and "unlisted".
    $status = new Google_VideoStatus();
    $status->privacyStatus = "unlisted";

    // Create a YouTube video with snippet and status
    $video = new Google_Video();
    $video->setSnippet($snippet);
    $video->setStatus($status);

    // Size of each chunk of data in bytes. Setting it higher leads faster upload (less chunks,
    // for reliable connections). Setting it lower leads better recovery (fine-grained chunks)
    $chunkSizeBytes = 1 * 1024 * 1024;

    // Create a MediaFileUpload with resumable uploads
    $media = new Google_MediaFileUpload('video/*', null, true, $chunkSizeBytes);
    $media->setFileSize(filesize($videoPath));

    // Create a video insert request
    $insertResponse = $youtube->videos->insert("status,snippet", $video,
        array('mediaUpload' => $media));

    $uploadStatus = false;

    // Read file and upload chunk by chunk
    $handle = fopen($videoPath, "rb");
    if($handle) {
        while (!$uploadStatus && !feof($handle)) {
          $chunk = fread($handle, $chunkSizeBytes);
          $uploadStatus = $media->nextChunk($insertResponse, $chunk);
            if($uploadStatus) {
                echo 'status true <BR />';
            }
            else { 
                echo 'status false <BR />';
            }
        }
    }
    else {
        exit("Unable to open the input file.");
    }

    if(!fclose($handle)) {
        echo '<br />close failed';
    }
    else {
        echo '<br />close successful';
    }

    $htmlBody .= "<h3>Video Uploaded</h3><ul>";
    $htmlBody .= sprintf('<li>%s (%s)</li>',
        $uploadStatus['snippet']['title'],
        $uploadStatus['id']);

    $htmlBody .= '</ul>';

  } catch (Google_ServiceException $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
  } catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>A client error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
  }

  $_SESSION['token'] = $client->getAccessToken();
} else {
  // If the user hasn't authorized the app, initiate the OAuth flow
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  $htmlBody = <<<END
  <h3>Authorization Required</h3>
  <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
}
?>

<!doctype html>
<html>
<head>
<title>Video Uploaded</title>
</head>
<body>
  <?=$htmlBody?>
</body>
</html>
1

1 Answers

0
votes

Maybe this will help someone else. The problem is that I was trying to upload a file directly from my hard drive. Everything worked fine when I first uploaded the file to the server and from there uploaded to youtube.