使用 youtube gdata API 检索直接上传的进度 [英] Retrieving progress of Direct Upload using youtube gdata API

查看:71
本文介绍了使用 youtube gdata API 检索直接上传的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注有关使用 gData Youtube API 使用 PHP 进行直接上传的文档.我可以成功上传视频,所以这一切都很好.我无法弄清楚的部分,如果它甚至可能获得一些上传进度状态,如完成百分比或上传的字节数.

这是发生上传的代码片段.

 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();}?>

任何人有任何想法如何更新 $progess 变量或其他东西,因为这是上传?

解决方案

Demo url:--

http://jquery.malsup.com/form/progress.html

您可以在url下面下载jquery文件并添加html标签

http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js

http://malsup.github.com/jquery.form.js

试试这个:--

输出视频:-

这是我的 video.php 文件

<头><title>文件上传进度演示#1</title><风格>身体 { 填充:30px }形式{显示:块;边距:20px 自动;背景:#eee;边框半径:10px;填充:15px }.progress { 位置:相对;宽度:400px;边框:1px 实心#ddd;填充:1px;边框半径:3px;}.bar { 背景色:#B4F5B4;宽度:0%;高度:20px;边框半径:3px;}.percent { 位置:绝对;显示:内联块;顶部:3px;左:48%;}</风格><身体><h1>文件上传进度演示#1</h1><code>&lt;input type="file" name="myfile"></code><form action="video_upload.php" method="post" enctype="multipart/form-data"><input type="file" name="uploadedfile"><br><input type="submit" value="上传文件到服务器"></表单><div class="progress"><div class="bar"></div ><div class="percent">0%</div >

<div id="状态"></div><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script><script src="http://malsup.github.com/jquery.form.js"></script><脚本>(功能() {var bar = $('.bar');var 百分比 = $('.percent');var status = $('#status');$('form').ajaxForm({发送前:函数(){状态.空();var percentVal = '0%';bar.width(percentVal)百分比.html(percentVal);},上传进度:功能(事件,位置,总计,百分比完成){var percentVal = percentComplete + '%';bar.width(percentVal)百分比.html(percentVal);},完成:功能(xhr){bar.width("100%");百分比.html("100%");status.html(xhr.responseText);}});})();</html>

这是我的 video_upload.php 文件

setVideoTitle($videoTitle);$myVideoEntry->setVideoDescription($VideoDescription);//该类别必须是有效的 YouTube 类别!$myVideoEntry->setVideoCategory($VideoCategory);//设置关键字.请注意,这必须是逗号分隔的字符串//并且单个关键字不能包含空格$myVideoEntry->SetVideoTags($VideoTags);$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=http://YOUR_WEBPAGE.com/");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);//与  相同$post = array("file"=>"@".$VideoFile['tmp_name'], "token"=>$tokenValue);curl_setopt($ch, CURLOPT_POSTFIELDS, $post);$response = curl_exec($ch);$info = curl_getinfo($ch);回声$信息;?>

I'm following along with the documentation about using the gData Youtube API for doing direct uploads with PHP. I can successfully upload videos, so thats all well and good. The part I'm having trouble figuring out, if its even possible is getting some upload progress status as in percent complete or number of bytes uploaded.

Here is the snippet of code where the upload happens.

 <?php 
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();
    }
?>

Anyone have any ideas how to update a $progess variable or something as this is uploading?

解决方案

Demo url:--

http://jquery.malsup.com/form/progress.html

You can download jquery file below url and add in html tag

http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js

http://malsup.github.com/jquery.form.js

Try this:--

output video:-

This is my video.php file

<!doctype html>
<head>
<title>File Upload Progress Demo #1</title>
<style>
body { padding: 30px }
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }

.progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
.bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
.percent { position:absolute; display:inline-block; top:3px; left:48%; }
</style>
</head>
<body>
    <h1>File Upload Progress Demo #1</h1>
    <code>&lt;input type="file" name="myfile"></code>
        <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>

    <div class="progress">
        <div class="bar"></div >
        <div class="percent">0%</div >
    </div>

    <div id="status"></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
(function() {

var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');

$('form').ajaxForm({
    beforeSend: function() {
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    complete: function(xhr) {
        bar.width("100%");
        percent.html("100%");
        status.html(xhr.responseText);
    }
}); 

})();       
</script>

</body>
</html>

this is my video_upload.php file

<?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 = "USERNAME",
              $password = "PASSWORD",
              $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 = 'YOUR DEVELOPER KEY';
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);



// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

$myVideoEntry->setVideoTitle($videoTitle);
$myVideoEntry->setVideoDescription($VideoDescription);
// The category must be a valid YouTube category!
$myVideoEntry->setVideoCategory($VideoCategory);

// Set keywords. Please note that this must be a comma-separated string
// and that individual keywords cannot contain whitespace
$myVideoEntry->SetVideoTags($VideoTags);

$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=http://YOUR_WEBPAGE.com/");
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;

?>

这篇关于使用 youtube gdata API 检索直接上传的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆