Zend framework, YouTube API, default title (Browserbased upload) - php

I got this code for uploading videos to my youtubeaccount from my website:
<?php
session_start();
set_time_limit(0);
ini_set('memory_limit', '150M');
ini_set('upload_max_filesize', '30M');
ini_set('default_socket_timeout', '6000');
ini_set('max_input_time', '6000');
ini_set('post_max_size', '100M');
ini_set('max_execution_time', '6000');
$clientLibraryPath = 'library';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $clientLibraryPath);
//include Zend Gdata Libs
require_once("Zend/Gdata/ClientLogin.php");
require_once("Zend/Gdata/HttpClient.php");
require_once("Zend/Gdata/YouTube.php");
require_once("Zend/Gdata/App/MediaFileSource.php");
require_once("Zend/Gdata/App/HttpException.php");
require_once('Zend/Uri/Http.php');
//yt account info
$yt_user = 'xx'; //youtube username or gmail account
$yt_pw = 'xx'; //account password
$yt_source = 'xx'; //name of application (can be anything)
//yt dev key
$yt_api_key = 'xx'; //your youtube developer key
//login in to YT
$authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
$username = $yt_user,
$password = $yt_pw,
$service = 'youtube',
$client = null,
$source = $yt_source, // a short string identifying your application
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);
$yt = new Zend_Gdata_YouTube($httpClient, $yt_source, NULL, $yt_api_key);
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$myVideoEntry->setVideoTitle('The Movie');
$myVideoEntry->setVideoDescription('My Test Movie');
$myVideoEntry->setVideoCategory('Autos');
$myVideoEntry->SetVideoTags('cars, funny');
$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://sabinequardon.dk';
// build the form
$form = '<form id="youtube_upload" action="'. $postUrl .'?nexturl='. $nextUrl .
'" method="post" enctype="multipart/form-data" target="uploader">'.
'<input id="title" name="video_title" type="text"/>'.
'<input id="file_upload" name="file_upload" type="file"/>'.
'<input name="token" type="hidden" value="'. $tokenValue .'"/>'.
'<input value="Upload Video File" type="submit" id="submit" />'.
'</form>
';
echo $form;
?>
I'm not really good at PHP, and it's the first time I'm trying to use the YouTube API. Been reading everything I could, just didn't seem to help much.
I know it's something that I get the metadata first before uploading the video?
I need the user to write their own title, so all the video's aren't named the same thing.
Is there a way to do this?
I've tried looking everywhere, just can't seem to find the right answer.
Just annoying if all the videos the users are uploading are called the same.
Thank you.

This line sets the video title:
$myVideoEntry->setVideoTitle('The Movie');
As you already have an input for the title in the form, you can simply use it:
$video_title = $_POST['video_title'];
$myVideoEntry->setVideoTitle($video_title);
Or simply:
$myVideoEntry->setVideoTitle($_POST['video_title']);

Related

TwitterOAuth from Abraham - Could not authenticate you; Error: 32

I am trying to use the TwitterOAuth solution from Abraham.
I've done everything as described in his documentation, but I still get that error:
{"errors":[{"code":32,"message":"Could not authenticate you."}]}
That's my source code:
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$consumerkey = 'xx';
$consumersecret = 'xx';
$accesstoken = 'xx';
$accesstokensecret = 'xx';
$connection = new TwitterOAuth($consumerkey,$consumersecret,$accesstoken,$accesstokensecret);
$tweets = $connection->get("search/tweets.json?q=superbowl");
echo json_encode($tweets);
All the keys are correct. I have no clue, why this still happens.
The App Permission are Read only.
What I want to do is get tweets based on a search query.
Do you have any idea how to fix this? Let me know, if you need some more information.
Hope this will fix your problem....
<form action='' method='get' ><input type='text' name='q'> <button type='submit' >click</button></form>
<?php
require "autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$consumerKey = "youconsumerkey";
$consumerSecret = "yourconsumersecretkey";
$oauthToken = "youoauthtoken";
$oauthTokenSecret = "youroauthtokensecret";
$twitter = new TwitterOAuth($consumerKey,$consumerSecret,$oauthToken,$oauthTokenSecret);
$search = isset($_GET['q']);
if(isset($_GET['q'])){
$tweets = $twitter->get('search/tweets',array('q'=>'%23'.$_GET['q'],'result_type'=>'recent','count'=>'10'));
//var_dump($tweets);
foreach($tweets->statuses as $value){
echo $value->user->name; echo "<br/>";
//echo json_encode($tweets);
}
}
?>
#
Actually in my case the reason behind ]Could not authenticate you; Error: 32]
was this line of code that i had tested for my project:
// $tweets = $twitter->get('search/tweets.json?q=mytest&result_type=recent&count=10');
I am Using TwitterOAuth PHP Library for the Twitter REST API, https://twitteroauth.com/ .
HTTP
GET https://api.twitter.com/1.1/search/tweets.json
TwitterOAuth
$tweets= $twitter->get("search/tweets.json", array("result_type" => 'recent', "count" => 10)); //should be like this
// $tweets= $twitter->get('search/tweets.json?q=mi_twitter_test&result_type=recent&count=10'); //throws authentication error

How to POST video to Facebook through Graph API using PHP

The code found in the documentation uses a <form method="POST" to post a video to a profile:
// Using the page access token from above, create the POST action
// that our form will use to upload the video.
$post_url = "https://graph-video.facebook.com/" . $page_id . "/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&access_token=". $access_token;
// Create a simple form
echo '<form enctype="multipart/form-data" action=" '.$post_url.' "
method="POST">';
echo 'Please choose a file:';
echo '<input name="file" type="file">';
echo '<input type="submit" value="Upload" />';
echo '</form>';
What's the cleanest way to post from a URL without using a form?
Assuming that you have already uploaded the video on your server...
$config = array();
$config['appId'] = 'appID';
$config['secret'] = 'secretID';
$config['fileUpload'] = true;
$config['cookie'] = true;
$facebook = new Facebook($config);
$facebook->setFileUploadSupport(true);
$video_details = array(
'access_token'=> 'user publish token',
'message'=> 'Test video!',
'source'=> '#' .realpath($videosPathOnServer)
);
$post_video = $facebook->api('/'.$usersFacebookID.'/videos', 'post', $video_details);
As far as I remember, by default all videos visibility is set to Friends and their Friends

Get Youtube video by tags from specific user

I'm trying to get a youtube video from the $username with $tags:
$tags='detskij-sad-198';
require_once('Zend/Loader.php');
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 = 'Schoolkharkovua',
$password = '*****',
$service = 'youtube',
$client = null,
$source = '*****',
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);
$devkey = '*****';
$yt = new Zend_Gdata_YouTube($httpClient, '', '', $devkey);
$yt->setMajorProtocolVersion(2);
$query = $yt->newVideoQuery();
$query->setMaxResults(4);
$query->setVideoQuery($tags); // also i tried $query->category = $tags;
$query->setAuthor($username);
$videoFeed = $yt->getVideoFeed($query->getQueryUrl(2));
$videoFeed returns no entry, although I know that the $username has a video with $tags and this code to work until mid-March
If I do query only by the $username or by $tags - I get the result.
What am I doing wrong?
PS. http://gdata.youtube.com/demo/index.html return empty video feed too if I trying query by "Keywords" and "Author name" simultaneously
Hhhhhmmmmm.... It seems that "google" no longer get the symbols "-" in tags
But
echo 'Tags: ' . implode(", ", $videoEntry->getVideoTags());
still emty, youtube api don't return video tags!
Guys help me get video tags from my video channel, please!
You need to authenticate your connection, YT returns an empty media$keywords object now (as of last Aug).
https://developers.google.com/youtube/2.0/developers_guide_php#Authentication
and here's their blog post about it...
http://apiblog.youtube.com/2012/08/video-tags-just-for-uploaders.html

Youtube video upload API issue

I just can't seem to get this API working. My eyes are missing the mistakes I'm making. The problem I have is that the page still Shows BLANK.
I need the form to upload the data onto Youtube which will later be displayed on my website.
Here is my code:
<?php
require_once 'library/Zend/Loader/AutoloaderFactory.php'; // the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
// start a new session
session_start();
function getAuthSubRequestUrl()
{
$next = 'http://www.example.com/welcome.php';
$scope = 'http://gdata.youtube.com';
$secure = false;
$session = true;
return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure, $session);
}
function getAuthSubHttpClient()
{
if (!isset($_SESSION['sessionToken']) && !isset($_GET['token']) ){
echo 'Login!';
return;
} else if (!isset($_SESSION['sessionToken']) && isset($_GET['token'])) {
$_SESSION['sessionToken'] = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
}
$httpClient = Zend_Gdata_AuthSub::getHttpClient($_SESSION['sessionToken']);
return $httpClient;
}
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient =
Zend_Gdata_ClientLogin::getHttpClient(
$username = 'myuser#gmail.com',
$password = 'mypassword',
$service = 'youtube',
$client = null,
$source = 'MySource', // a short string identifying your application
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);
$developerKey = 'ABC123 ... ';
$applicationId = 'Video uploader v1';
$clientId = 'My video upload client - v1';
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
// Assuming that $yt is a valid Zend_Gdata_YouTube object
$yt->setMajorProtocolVersion(2);
function getAndPrintVideoFeed($location = Zend_Gdata_YouTube::VIDEO_URI)
{
$yt = new Zend_Gdata_YouTube();
// set the version to 2 to receive a version 2 feed of entries
$yt->setMajorProtocolVersion(2);
$videoFeed = $yt->getVideoFeed($location);
printVideoFeed($videoFeed);
}
function printVideoFeed($videoFeed)
{
$count = 1;
foreach ($videoFeed as $videoEntry) {
echo "Entry # " . $count . "\n";
printVideoEntry($videoEntry);
echo "\n";
$count++;
}
}
// 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);
// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$myVideoEntry->setVideoTitle('My Test Movie');
$myVideoEntry->setVideoDescription('My Test Movie');
// The category must be a valid YouTube category!
$myVideoEntry->setVideoCategory('Autos');
// Set keywords. Please note that this must be a comma-separated string
// and that individual keywords cannot contain whitespace
$myVideoEntry->SetVideoTags('cars, funny');
$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://www.example.com/youtube_uploads';
// build the form
$form = '<form action="'. $postUrl .'?nexturl='. $nextUrl .
'" method="post" enctype="multipart/form-data">'.
'<input name="file" type="file"/>'.
'<input name="token" type="hidden" value="'. $tokenValue .'"/>'.
'<input value="Upload Video File" type="submit" />'.
'</form>';
echo "aaaa".$form;
?>
Here is my code with Zend
//include Zend Gdata Libs
require_once("Zend/Gdata/ClientLogin.php");
require_once("Zend/Gdata/HttpClient.php");
require_once("Zend/Gdata/YouTube.php");
require_once("Zend/Gdata/App/HttpException.php");
require_once('Zend/Uri/Http.php');
//yt account info
$yt_user = 'myuser#gmail.com'; //youtube username or gmail account
$yt_pw = 'mypassword'; //account password
$yt_source = 'title of my video'; //name of application (can be anything)
//yt dev key get here : https://code.google.com/apis/youtube/dashboard/
$yt_api_key = ''ABC123 ... ''; //your youtube developer key
//login in to YT
$authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
$username = $yt_user,
$password = $yt_pw,
$service = 'youtube',
$client = null,
$source = $yt_source, // a short string identifying your application
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);
// 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);
$yt = new Zend_Gdata_YouTube($httpClient, $yt_source, NULL, $yt_api_key);
I can Upload my file and after a while I'm redirected to "$nextUrl".
My issue is that I get this page : http://www.example.com/youtube_uploads?statu=400&error=failed
And I don't no why.
Any Idea, please ?

CodeIgniter: Upload Video to YouTube

I'm using CodeIgniter + Zend libraries. I want to let users upload videos to my site, which I will then upload to my YouTube channel. This is my first exposure to the YouTube API. Can someone point me in the right direction?
Am I right to be looking at this: http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html#Direct_uploading? Does anyone have a snippet of code that shows how uploading is done via PHP?
Try this its working
class Addvideo extends Controller {
function Addvideo()
{
parent::Controller();
$this->load->library("zend");
$this->zend->load("Zend/Gdata/AuthSub");
$this->zend->load("Zend/Gdata/ClientLogin");
$authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
/************Authentication**************/
$this->zend->load("Zend/Gdata/YouTube");
$this->zend->load("Zend/Gdata/HttpClient");
$this->zend->load("Zend/Gdata/App/MediaFileSource");
$this->zend->load("Zend/Gdata/App/HttpException");
$this->zend->load("Zend/Uri/Http");
$youtube = new Zend_Gdata_YouTube();
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$Gdata_AuthSub=new Zend_Gdata_AuthSub();
$this->load->helper("text");
}
function index(){
$data['result']='videos';
$this->load->view('videos/newform', $data);
}
function indexed(){
$title = 'GruppoDSE Video';
$description = 'Description';
$youtube = new Zend_Gdata_YouTube();
$authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
$username = $this->config->item('username'),
$password = $this->config->item('password'),
$service = 'youtube',
$client = null,
$source = 'Arts Connector', // a short string identifying your application
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);
$developerKey = $this->config->item('developer_key');
$applicationId = 'Arts Connector';
$clientId = 'My video upload client - v1';
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$myVideoEntry->setVideoTitle($title);
$myVideoEntry->setVideoDescription($description);
// The category must be a valid YouTube category!
$myVideoEntry->setVideoCategory('Autos');
// Set keywords. Please note that this must be a comma-separated string
// and that individual keywords cannot contain whitespace
$myVideoEntry->SetVideoTags('cars, funny');
$tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
$tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);
$data['tokenValue'] = $tokenArray['token'];
$data['postUrl'] = $tokenArray['url'];
// place to redirect user after upload
$data['nextUrl'] = 'http://www.avantajsoftwares.com/gruppo/uploadVideo';
$res="Il file video aggiungere con successo, il risultato atteso verrĂ  visualizzato dopo alcune volte";
$this->session->set_userdata('greenFlag',$res);
// build the form
$this->load->view('videos/form', $data);
}
function AddVideoAjax(){
$title=$_POST['title'];
$description=$_POST['description'];
$youtube = new Zend_Gdata_YouTube();
$authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
$username = $this->config->item('username'),
$password = $this->config->item('password'),
$service = 'youtube',
$client = null,
$source = 'Arts Connector', // a short string identifying your application
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);
$developerKey = $this->config->item('developer_key');
$applicationId = 'Arts Connector';
$clientId = 'My video upload client - v1';
try {
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$myVideoEntry->setVideoTitle($title);
$myVideoEntry->setVideoDescription($description);
// The category must be a valid YouTube category!
$myVideoEntry->setVideoCategory('Education');
// Set keywords. Please note that this must be a comma-separated string
// and that individual keywords cannot contain whitespace
$myVideoEntry->SetVideoTags('Seminar, Events');
$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://www.avantajsoftwares.com/gruppo/Addvideo';
$res="Il file video aggiungere con successo, il risultato atteso verrĂ  visualizzato dopo alcune volte";
$this->session->set_userdata('greenFlag',$res);
// build the form
echo $form = '<form action="'. $postUrl .'?nexturl='. $nextUrl .
'" method="post" enctype="multipart/form-data" onsubmit="return valid()">'.
'<div style="float:left;"><input name="file" type="file" id="file"/></div><div class="preloader"></div>'.
'<input name="token" type="hidden" value="'. $tokenValue .'"/>'.
'<div style="clear:both"></div>'.
'<div class="login-footer" id="prog_bar style="cursor:pointer"><input value="Carica video" type="submit" id="validate" class="button" onclick="progress_bar()"/></div>'.
'</form>';
} catch (Zend_Gdata_App_Exception $e) {
echo $return="<div class='login-footer' style='width:130px;'><a style='height:16px; padding-top:7px;' class='button' href=".site_url()."Addvideo>Riprova di nuovo</a></div>";
}
// Assuming that $videoEntry is the object that was returned during the upload
//$state = $myVideoEntry->getVideoState();
}
function getAuthSubRequestUrl()
{
$gdata_AuthSub = new Zend_Gdata_AuthSub();
$next = 'http://www.avantajsoftwares.com/gruppo/videos';
$scope = 'http://gdata.youtube.com';
$secure = false;
$session = true;
return $data=$gdata_AuthSub->getAuthSubTokenUri($next, $scope, $secure, $session);
}
function getAuthSubHttpClient()
{
echo $_SESSION['sessionToken'];
if (!isset($_SESSION['sessionToken']) && !isset($_GET['token']) ){
echo 'Login!';
return;
} else if (!isset($_SESSION['sessionToken']) && isset($_GET['token'])) {
$_SESSION['sessionToken'] = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
}
$httpClient = Zend_Gdata_AuthSub::getHttpClient($_SESSION['sessionToken']);
return $httpClient;
}
/*************************** Class end ***********************************/
}
I don't recommend using Zend with CI due to it's size and complexity. I developed a library specifically for CodeIgniter https://github.com/jimdoescode/CodeIgniter-YouTube-API-Library
The library provides several different options for uploading to YouTube. You can do a direct upload where you upload a video stored on your server to youtube. There is also an option to use a form to upload a video from the client to youtube. Check it out and let me know if you have any questions.
The docs have this, as well as other methods, covered for Zend Gdata: Uploading Videos

Categories