4

我的本地主机上有一个页面,用户可以从中上传视频。现在我想实现该功能,管理员可以将这些视频公开(用户必须将它们作为私人视频上传)。现在为此,首先我需要获取频道上所有视频的列表(包括私人视频)。我试过这段代码:

    $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">
                </entry>';
    $headers = array( "Authorization: GoogleLogin auth=".$authvalue,
                 "GData-Version: 2",
                 "X-GData-Key: key=".$youtube_key,
                 "Content-length: ".strlen( $data ),
                 "Content-Type: application/atom+xml; charset=UTF-8");
    $curl = curl_init( "https://gdata.youtube.com/feeds/api/users/default/uploads");
    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 = curl_exec($curl);
 curl_close($curl);

返回此错误:

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:....

我找不到请求用户视频列表的正确格式的标题,所以我使用了我申请上传视频的标题(并且有效,所以我确定用户身份验证没有问题)。

如果你能告诉我我做错了什么,或者只是给我指出一个工作视频列表的例子,我将非常感激。

编辑: curl_error 返回一个空字符串, curl_info 返回这个:

array (size=26)
  'url' => string 'https://uploads.gdata.youtube.com/feeds/api/users/.../uploads' (length=80)
  'content_type' => string 'text/html; charset=UTF-8' (length=24)
  'http_code' => int 400
  'header_size' => int 597
  'request_size' => int 1747
  'filetime' => int -1
  'ssl_verify_result' => int 20
  'redirect_count' => int 1
  'total_time' => float 0.639
  'namelookup_time' => float 0.047
  'connect_time' => float 0.078
  'pretransfer_time' => float 0.172
  'size_upload' => float 0
  'size_download' => float 925
  'speed_download' => float 1447
  'speed_upload' => float 0
  'download_content_length' => float 925
  'upload_content_length' => float 0
  'starttransfer_time' => float 0.203
  'redirect_time' => float 0.436
  'certinfo' => 
    array (size=0)
      empty
  'primary_ip' => string '173.194.70.117' (length=14)
  'primary_port' => int 443
  'local_ip' => string '10.0.0.33' (length=9)
  'local_port' => int 53639
  'redirect_url' => string '' (length=0)
4

2 回答 2

1

参考上面的评论,使用Zend_Gdata ( version >= 1.7.7 ) 扩展安装 Zend 库解决了这个问题。还要注意 PHP 版本必须 >= 5.1.4 。

参考:Google YouTube API - PHP

于 2012-12-28T02:29:20.507 回答
0

我认为您必须在此 cURL 调用中发送的每个参数上尝试 php trim 功能。像

"Authorization: GoogleLogin auth=".trim($authvalue),
                 "GData-Version: 2",
                 "X-GData-Key: key=".trim($youtube_key),
                 "Content-length: ".trim(strlen($data)),

它应该工作。

于 2012-12-27T12:51:53.130 回答