3

how can i get the video-id of a just uploaded Youtube movie?

I'm using this code:

$yt = new Zend_Gdata_YouTube($httpClient);
// create a new Zend_Gdata_YouTube_VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

// create a new Zend_Gdata_App_MediaFileSource object
$filesource = $yt->newMediaFileSource('mytestmovie.mov');
$filesource->setContentType('video/quicktime');
// set slug header
$filesource->setSlug('mytestmovie.mov');

// add the filesource to the video entry
$myVideoEntry->setMediaSource($filesource);

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

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

// upload URI for the currently authenticated user
$uploadUrl = 'http://uploads.gdata.youtube.com/feeds/api/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();
}

I assume it 'll be a property of $newEntry but i can't seem to find it!

Thanks!

Charles
  • 50,943
  • 13
  • 104
  • 142
murze
  • 4,015
  • 8
  • 43
  • 70

1 Answers1

4

As you can see on the page you took this code from, right below, $newEntry->getVideoId() will contain the ID. You can then check what its status is (uploaded, processed):

// Assuming that $newEntry is the object that was returned during the upload
$state = $newEntry->getVideoState();

if ($state) {
  echo 'Upload status for video ID ' . $newEntry->getVideoId() . ' is ' .
    $state->getName() . ' - ' . $state->getText() . "\n";
  } else {
    echo "Not able to retrieve the video status information yet. " . 
      "Please try again later.\n";
}
Arda Xi
  • 2,616
  • 3
  • 19
  • 24
  • Call me dumb but I'm having trouble integrating this piece of code into the page. Exactly how do you implement this? – Tom Jan 20 '11 at 19:29
  • @Tom - You will want to implement your uploading code first, and make sure you replace the `$newEntry` with wherever you stored the object that was returned by the upload. For more information on the uploading code itself, see the link in my post. – Arda Xi Jan 20 '11 at 19:55
  • My issue is with storage. I've got it set for the new URL to refresh to my page, so it returns http://example.com?status=200&id=JPF-DXF7hzc - How do I "save" this value? – Tom Jan 20 '11 at 20:22
  • @Tom - I'm not sure what you're asking. If you mean how to store the ID, there are many ways in PHP, but those are out of scope for this question. – Arda Xi Jan 20 '11 at 21:16
  • This answer was useful but I also needed to implement the workaround shown here to remove the error: https://groups.google.com/forum/?fromgroups=#!topic/youtube-api-gdata/fYbKwNP5Dj8 – Andrew Mar 11 '13 at 02:52