php - YouTube API-直接上传-413请求实体太大

标签 php sockets upload youtube-api

我一直在尝试通过直接上传将视频上传到YouTube API。我终于破解了OAuth流程,并且有了一个有效的 token 。我真的只需要对YouTube做两件事:验证和上传。我没有浏览或使用任何其他功能。用户将视频上传到此网站,我将它们发送到YouTube进行播放。

我觉得自己已经80-90%地来到这里了,所以我不想废弃它并使用Google提供的Zend库。

问题:
当我发送请求时,我得到以下响应:

HTTP/1.1 413 Request Entity Too Large
Content-Type: text/html; charset=UTF-8
Content-Length: 171
Date: Thu, 18 Apr 2013 18:33:22 GMT
Expires: Thu, 18 Apr 2013 18:33:22 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Connection: close

如果打开警告,还会收到“管道破裂”警告,这可能只是试图通过关闭/拒绝的连接上传数据的代码。

我的要求是这样的:
POST /feeds/api/users/default/uploads HTTP/1.1 
Host: gdata.youtube.com Connection: close 
User-Agent: PHP Accept-encoding: identity 
Authorization: Bearer <TOKEN IS HERE> 
GData-Version: 2.0 
X-GData-Key: key=<MYKEYISHERE>
Slug: throwing_can.mp4 
Content-Type: multipart/related; boundary="5170429d1b193" 
Content-Length: 3920610 

文件本身被分块并写入流中。
function ytapi_write_file($handle, $path, $chunksize = 8192){
    $filehandle = fopen($path, 'r');
    while(!feof($filehandle)){
        fwrite($handle, fread($filehandle, $chunksize));
    }
    fclose($filehandle);
    $filehandle = null;
}

function ytapi_write($handle, $request){
    fwrite($handle, $request);
    return $request;
}

像这样
ytapi_write($handle, $start); //This writes the header.
ytapi_write_file($handle, $path, 8192); //This writes the file raw/binary.
ytapi_write($handle, $end); //This writes the final boundary.

另外,我将其用于标题信息:
$_header = array(
    'Host'=>'gdata.youtube.com',
    'Connection'=>'close',
    'User-Agent'=>'PHP',
    'Accept-encoding'=>'identity'
);

关于我在做什么错的任何想法吗?如果需要,我可以提供更多信息。我正在上传的文件超过3MB,该文件位于相关服务器上。我已验证位置正确。

更新

更改为uploads.gdata.youtube.com

现在,我收到此错误消息:
Host: uploads.gdata.youtube.com
Connection: close
User-Agent: PHP
Accept-encoding: identity
tcp://uploads.gdata.youtube.com:80HTTP/1.1 400 Bad Request
Server: HTTP Upload Server Built on Apr 8 2013 13:06:58 (1365451618)
X-GData-User-Country: US
Content-Type: application/vnd.google.gdata.error+xml
X-GUploader-UploadID: <SOME ID>
Date: Thu, 18 Apr 2013 19:42:14 GMT
Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Cache-Control: no-cache, no-store, must-revalidate
Content-Length: 228
Connection: close

最佳答案

这是我的实现:

$data = '<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">' . $title . '</media:title>
        <media:description type="plain">' . trim($description) . '</media:description>
        <media:keywords>' . $tags . '</media:keywords>
        <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Music</media:category>
    </media:group>
</entry>';
$headers = array(
    'Authorization: GoogleLogin auth=' . $auth,
    'GData-Version: 2',
    'X-GData-Key: key=<KEY>',
    'Slug: ' . basename($videoPath)
);

$tmpfname = tempnam("/tmp", "youtube");
$handle = fopen($tmpfname, "w");
fwrite($handle, $data);
fclose($handle);

$post = array(
    'xml' => '@' . $tmpfname . ";type=application/atom+xml",
    'video' => '@' . $videoPath . ";type=video/mp4",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "uploads.gdata.youtube.com/feeds/api/users/default/uploads");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);

关于php - YouTube API-直接上传-413请求实体太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16091432/

相关文章:

php - PHP 中长命令的 exec() 问题

java - 如何发送二进制格式的Java对象并在C服务器中接收?

php - 使用字符串路径设置嵌套数组数据

sockets - Golang 原始套接字丢失数据包?

具有多个接口(interface)的 Python UDP 套接字

.net - 在 ASP.NET MVC 中上传文件(再次!)

c# - 将大图像文件上传到 SharePoint 时出现问题

php - 无法在 php/mysql 中上传图片

php - 使用 fishpig 在 magento 类别页面上显示相关的博客文章/博客类别

php - 减少圈复杂度的过多 switch case 的替代方案?