Josh Randall
Josh Randall

Reputation: 1344

How to upload video to Youtube using zend gdata

I have logged in the user. I have retrieved his favourite videos. But when I try to upload videos I get an error

Fatal error File to be uploaded at does not exist or is not readable.

This is the code I use to upload video

         $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
         $file = '/files/trainingvideo1.mp4';
         $file = realpath($file);  
         $filesource = $yt->newMediaFileSource($file); 
         $filesource->setContentType('video/mp4'); 
         $filesource->setSlug($file);  
         $myVideoEntry->setMediaSource($filesource);  
         $myVideoEntry->setVideoTitle('Tutorial 1'); 
         $myVideoEntry->setVideoDescription('Tutorial 1'); 
         $myVideoEntry->setVideoCategory('Entertainment');  
         $myVideoEntry->SetVideoTags('testme');  
         $myVideoEntry->setVideoDeveloperTags(array('tester', 'test'));
         $uploadUrl = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads';
         $newEntry = $yt->insertEntry($myVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry');

I appreciate any help.

Upvotes: 0

Views: 2109

Answers (1)

swiecki
swiecki

Reputation: 3483

Edit: With the information that your realpath() is returning false, we can assume that you're probably configuring zend gdata correctly and just passing in a bad file.

Here is the PHP documentation on realpath(): http://php.net/manual/en/function.realpath.php

The part that matters is:

realpath() returns FALSE on failure, e.g. if the file does not exist.

Note:

The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return FALSE.

So at this point I would go ahead and:

  1. Check that the file exists and your URL is correct- perhaps try using an absolute URL at first to test and build up to a relative one progressively
  2. Check permissions on the file, make sure it is executable by all (755 unix permissions, I think)

Good Luck!

Zend Gdata Youtube Doc:

Uploading videos can be done in one of two ways: either by uploading the video directly or by sending just the video meta-data and having a user upload the video through an HTML form.

In order to upload a video directly, you must first construct a new » Zend_Gdata_YouTube_VideoEntry object and specify some required meta-data.

The code below creates a blank » Zend_Gdata_YouTube_VideoEntry to be uploaded. A » Zend_Gdata_App_MediaFileSource object is then used to hold the actual video file. Under the hood, the » Zend_Gdata_YouTube_Extension_MediaGroup object is used to hold all of the video's meta-data. The $uploadUrl is the location where the new entry gets posted to. This can be specified either with the $userName of the currently authenticated user, or, alternatively, you can simply use the string 'default' to refer to the currently authenticated user.

$yt = new Zend_Gdata_YouTube($httpClient);
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

$filesource = $yt->newMediaFileSource('mytestmovie.mov');
$filesource->setContentType('video/quicktime');
$filesource->setSlug('mytestmovie.mov');

$myVideoEntry->setMediaSource($filesource);

$myVideoEntry->setVideoTitle('My Test Movie');
$myVideoEntry->setVideoDescription('My Test Movie');
// Note that category must be a valid YouTube category !
$myVideoEntry->setVideoCategory('Comedy');

// Set keywords, note that this must be a comma separated string
// and that each keyword cannot contain whitespace
$myVideoEntry->SetVideoTags('cars, funny');

// Optionally set some developer tags
$myVideoEntry->setVideoDeveloperTags(array('mydevelopertag',
                                           'anotherdevelopertag'));

// Optionally set the video's location
$yt->registerPackage('Zend_Gdata_Geo');
$yt->registerPackage('Zend_Gdata_Geo_Extension');
$where = $yt->newGeoRssWhere();
$position = $yt->newGmlPos('37.0 -122.0');
$where->point = $yt->newGmlPoint($position);
$myVideoEntry->setWhere($where);

// Upload URI for the currently authenticated user
$uploadUrl =
    'http://uploads.gdata.youtube.com/feeds/users/default/uploads';

// Try to upload the video, catching a Zend_Gdata_App_HttpException
// if available or just a regular Zend_Gdata_App_Exception

try {
    $newEntry = $yt->insertEntry($myVideoEntry,
                                 $uploadUrl,
                                 'Zend_Gdata_YouTube_VideoEntry');
} catch (Zend_Gdata_App_HttpException $httpException) {
    echo $httpException->getRawResponseBody();
} catch (Zend_Gdata_App_Exception $e) {
    echo $e->getMessage();
}

To upload a video as private, simply use: $myVideoEntry->setVideoPrivate(); prior to performing the upload. $videoEntry->isVideoPrivate() can be used to check whether a video entry is private or not.

Source: http://framework.zend.com/manual/en/zend.gdata.youtube.html

Upvotes: 1

Related Questions