PHP从URL上传到Youtube,而不是从表单上传

时间:2013-06-23 22:41:08

标签: php youtube youtube-api

我已经下载了一个脚本,可以将视频上传到YouTube,而且效果也很完美。

我的问题是,google的参数是什么,只有通过php添加视频的正确代码是什么,而不是表单元素?

离。

$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" />

希望有人可以提供帮助!

1 个答案:

答案 0 :(得分:0)

无法上传视频,只能传递视频存储在其他某个网络服务器上的位置的网址。您需要拥有视频的本地副本,并且需要在上传请求中包含视频的所有字节。所以这是你需要照顾的第一件事 - 制作视频的本地副本。

您应该做的第二件事是使用"Zend_Gdata" PHP client library作为YouTube GData API并使用the example in this developers guide,因为它会大大简化您的代码。