php - PHP从URL而非表单上传到Youtube

标签 php youtube youtube-api

我已经下载了一个脚本来将视频上传到YouTube,它也可以完美运行。

我的问题是,google的参数是什么,什么是仅通过php而非通过form元素添加视频的正确代码?

例如

$videoURL = "Http://myurl.com/video.mp4"; 

这是代码:
<?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');

$accountType = 'HOSTED_OR_GOOGLE';
$youtube_email    = 'xxxx'; //youtube username or gmail account
$youtube_password      = 'xxxxx'; //account password
$source  = urlencode('ps'); //name of application (can be anything) // a short string identifying your application  
$key  = 'AI39si7OKa6-enYv34I7rm68sVaGHeHAK1fhe6UDx0Qh3Z8DEXr9_SBConY4TJ9HO7mLnZVlVl0xoVGPVEjZPMS6mDmdG0WB9g'; //your youtube developer key
$authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin'; 

$postdata = "Email=".$youtube_email."&Passwd=".$youtube_password."&service=youtube&source=$source";
$curl = curl_init("https://www.google.com/youtube/accounts/ClientLogin");
curl_setopt($curl, CURLOPT_HEADER, "Content-Type:application/x-www-form-urlencoded");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
$response = curl_exec($curl);
curl_close($curl);


list($auth, $youtubeuser) = explode("\n", $response);
list($authlabel, $authvalue) = array_map("trim", explode("=", $auth));
list($youtubeuserlabel, $youtubeuservalue) = array_map("trim", explode("=", $youtubeuser));

$youtube_video_title = "sdfsdfdf"; // This is the uploading video title.
$youtube_video_description = "sdfdfdfdf"; // This is the uploading video description.
$youtube_video_category = "Film"; // This is the uploading video category.
$youtube_video_keywords = "devil"; // This is the uploading video keywords.


$data = '<?xml version="1.0"?>
        <entry xmlns="http://www.w3.org/2005/Atom"
          xmlns:media="http://search.yahoo.com/mrss/"
          xmlns:yt="http://gdata.youtube.com/schemas/2007">
          <media:group>
            <media:title type="plain">'.$youtube_video_title.'</media:title>
            <media:description type="plain">'.$youtube_video_description.'</media:description>
            <media:category
              scheme="http://gdata.youtube.com/schemas/2007/categories.cat">'.$youtube_video_category.'</media:category>
            <media:keywords>'.$youtube_video_keywords.'</media:keywords>
          </media:group>
        </entry>';


$headers = array("Authorization: GoogleLogin auth=".$authvalue,
             "GData-Version: 2",
             "X-GData-Key: key=".$key,
             "Content-length: ".strlen($data),
             "Content-Type: application/atom+xml; charset=UTF-8");

$curl = curl_init("http://gdata.youtube.com/action/GetUploadToken");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_REFERER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);

$response = simplexml_load_string(curl_exec($curl));
echo $response;
curl_close($curl);


$rUrl = $response->url;
$nexturl = urlencode('http://pointsafari.dk/blablabla.php');
?>

<form action="<?php echo $rUrl; ?>?nexturl=<?php echo $nexturl; ?>" method="post" enctype="multipart/form-data" >
  <input id="file" type="file" name="file"/>
  <div id="errMsg" style="display:none;color:red">
    You need to specify a file.
  </div>
  <input type="hidden" name="token" value="<?php echo($response->token); ?>"/>

   <input type="submit" value="go" />

希望有人能帮忙!

最佳答案

无法上传视频,而只能传入视频在其他Web服务器上存储位置的URL。您需要具有视频的本地副本,并且需要在上传请求中包含视频的所有字节。因此,这是您需要做的第一件事-制作视频的本地副本。

您应该做的第二件事是将"Zend_Gdata" PHP client library用于YouTube GData API并遵循the example in this developers guide,因为这将大大简化您的代码。

关于php - PHP从URL而非表单上传到Youtube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17265986/

相关文章:

php - 让 Apache/PHP 根据作为 PHP 执行的文件的文件扩展名设置 MIME 类型

php - ON DUPLICATE KEY 忽略 ID

javascript - 修复Youtube API数据分页v3

javascript - 解码 youtube base.js 密码 2016

php - 发生客户端错误 : Could not create storage directory:/tmp/Google_Client/00

php - 使用 simplepie 从 RSS 提要获取图像 url

javascript - 在 YouTube 页面中发生转换后获取实际的 HTML(chrome 扩展)

jquery - 如何使用jquery插件在播放结束时重新显示视频预览帧 "tubeplayer"

javascript - YouTube API 播放列表随机播放不起作用?

php - 如何通过电子邮件发送 mysql 获取记录 php 页面