Reputation: 861
I am working on youtube browser based video upload. but its throwing an error.
Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with message 'Expected response code 200, got 400 <?xml version='1.0' encoding='UTF-8'?><errors><error><domain>yt:validation</domain><code>invalid_value</code><location type='xpath'>media:group/media:category[@scheme='http://gdata.youtube.com/schemas/2007/categories.cat']/text()</location></error></errors>' in /home/xxx/public_html/yyy.com/up/Zend/Gdata/App.php:714 Stack trace: #0 /home/xxx/public_html/yyy.com/up/Zend/Gdata.php(219): Zend_Gdata_App->performHttpRequest('POST', 'http://gdata.yo...', Array, '<atom:entry xml...', 'application/ato...', NULL) #1 /home/xxx/public_html/yyy.com/up/Zend/Gdata/App.php(905): Zend_Gdata->performHttpRequest('POST', 'http://gdata.yo...', Array, '<atom:entry xml...', 'application/ato...') #2 /home/xxx/public_html/yyy.com/up/Zend/Gdata/YouTube.php(704): Zend_Gdata_App->post(Object(Zend_Gdata_YouTube_VideoEntry), 'http://gdata.yo...') #3 /home/xxx/public_html/yyy.com/up/video_upload. in /home/xxx/public_html/yyy.com/up/Zend/Gdata/App.php on line 714
Code:
<?php
// video_upload.php
require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();
// Define the authentication that will be used
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
// Authenticate
$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient =
Zend_Gdata_ClientLogin::getHttpClient(
$username = "[email protected]",
$password = "example",
$service = 'youtube',
$client = null,
$source = 'HTML SOURCE CODE SNIPPET',
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);
$applicationId = 'YOUR APPLICATION ID';
$clientId = 'Upload videos to youtube using the youtube API';
$developerKey = 'A...........................0';
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$myVideoEntry->setVideoTitle('My');
$myVideoEntry->setVideoDescription('video');
// The category must be a valid YouTube category!
$myVideoEntry->setVideoCategory('news');
// Set keywords. Please note that this must be a comma-separated string
// and that individual keywords cannot contain whitespace
$myVideoEntry->SetVideoTags('news');
$tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
$tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);
$tokenValue = $tokenArray['token'];
$postUrl = $tokenArray['url'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl."?nexturl=//example.com/up");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file">
$post = array("file"=>"@".$VideoFile['tmp_name'], "token"=>$tokenValue);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
echo $info;
?>
Form:
<form action="video_upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadedfile"><br>
<input type="submit" value="Upload File to Server">
</form>
I want to upload video directly to my youtube account.so users can upload video to my youtube account..
So i m not adding anything to $applicationId
and $clientId
.
I think something need to be done with $applicationId
and $clientId
.
Thanks
UPDATE:
As Wanny Miarelli suggested
i changed news
to News
and now no error is showing, but video also not uploaded.
tried print_r($info)
and got this
Array
(
[url] => http://uploads.gdata.youtube.com/action/FormDataUpload/AIwbFAS1SkX04C09Re5sWeYXb562vc7W5KHcRu0uZcJ7v-A4nX_21zVwIOxdeBtp_s4tj-UcnZxJaN-Zd7nKVUfWwtWxW2YEPg?nexturl=//example.com/up
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.024856
[namelookup_time] => 0.018251
[connect_time] => 0.024751
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[certinfo] => Array
(
)
[redirect_url] =>
)
Upvotes: 1
Views: 1147
Reputation: 744
Yes, seems a category problem. Try to change " news " to " News "
Look there for a complete category List
https://developers.google.com/youtube/2.0/reference#Region_specific_feeds
this is the direct download to the .cat File
http://gdata.youtube.com/schemas/2007/categories.cat
this is what Goolge should send you after video upload ( success or fail )
After uploading the video file, the user is redirected to the nexturl specified in your form. If the upload was successful, YouTube appends id and status parameters to the URL as shown in the following example:
http://www.example.com/youtube_uploads?status=200&id=JPF-DXF7hzc If the upload was not successful, YouTube appends status and code parameters to the URL to help you diagnose the cause of the failure. The reference guide provides more information about these parameters.
Ok, i jsut set up a zend framework istance, and this code works for me
*THIS CODE IS EXPLANATORY PURPOSES ONLY- NOT SECURE AND NOT OPTIMAL *
The user wants to upload the video with a One Step Form, this is the solution
<?php
//single file script, need to know if we are in a POST or GET request, this needs to be changed, dividing the script into a separate file
if(isset($_FILES['file']['name'])){
Zend_Loader::loadClass('Zend_Gdata_YouTube');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
$username = '',
$password = '',
$service = 'youtube',
$client = null,
$source = 'TestApp', // a short string identifying your application
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);
$applicationId = 'TestAPP';
$developerKey = "---";
$clientId = "";
// Note that this example creates an unversioned service object.
// You do not need to specify a version number to upload content
// since the upload behavior is the same for all API versions.
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$myVideoEntry->setVideoTitle($_POST['Title']);
$myVideoEntry->setVideoDescription($_POST['Description']);
// The category must be a valid YouTube category!
$myVideoEntry->setVideoCategory('News');
// Set keywords. Please note that this must be a comma-separated string
// and that individual keywords cannot contain whitespace
$myVideoEntry->SetVideoTags('News');
$tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
$tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);
$tokenValue = $tokenArray['token'];
$postUrl = $tokenArray['url'];
// place to redirect user after upload
$nextUrl = 'http://localweb/';
$target_url = 'http://127.0.0.1/accept.php';
//This needs to be the full path to the file you want to send.
$file_name_with_full_path = realpath('C:\video.flv');
/* curl will accept an array here too.
* Many examples I found showed a url-encoded string instead.
* Take note that the 'key' in the array will be the key that shows up in the
* $_FILES array of the accept script. and the at sign '@' is required before the
* file name.
*/
// Post the video to Youtube Server
$post = array('token' => $tokenValue,'file_contents'=>'@'. $_FILES["file"]["tmp_name"]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl . '?nexturl=' . $nextUrl);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
}else{
$self = $_SERVER['PHP_SELF'];
$form = '<form action="' . $self . '"'.
' method="post" enctype="multipart/form-data">'.
'<input name="file" id="file" type="file"/>'.
'<input name="token" id="token" type="hidden" value=""/>'.
'<input name="Description" id="Description" />'.
'<input name="Title" id="Title" />'.
'<input value="Upload Video File" type="submit" />'.
'</form>';
echo $form;
}
Upvotes: 1