【问题标题】:Picasa image/video resumable upload issuePicasa 图片/视频可恢复上传问题
【发布时间】:2015-06-27 05:58:19
【问题描述】:

我正在尝试使用 google api 可恢复上传选项上传视频/图像。

如果我有 200MB 的文件,那么我必须进行 N 次块上传。 我的代码 N-1 次返回 308 状态代码(最后一次除外)。对于最后一块谷歌没有回应我(我的 php 服务器说“empty-reply-from-server”)

请帮助我哪里错了。

global $access_token;
$title = basename($videoPath);
$mimeType = mime_content_type($videoPath);
$content_length = getSize($videoPath);
$curl = new Curl();
$curl->setHeader("Host", "picasaweb.google.com");
$curl->setHeader("Content-Length", 0);
$curl->setHeader("X-upload-content-length", $content_length);
$curl->setHeader("X-upload-content-type", $mimeType);
$curl->setHeader("Slug", $title);
$curl->setHeader("Authorization", "Bearer $access_token");
$curl->setHeader("Content-Type", 'application/atom+xml');
$curl->setHeader("User-Agent", "CXchange-CXchange-1.0 cURL/1.11.3");
$curl->setHeader("Accept-encoding", "identity");
$curl->setHeader("GData-Version", "2");
$curl->setHeader("MIME-version", "1.0");
try {
    //Get the resumable uri
    $url = "https://picasaweb.google.com/data/upload/resumable/media/create-session/feed/api/user/default/albumid/$id";
    $responce = $curl->post($url);
    $responce_code = $curl->http_status_code;
    if ($responce_code == 200) {
        $headers = $curl->response_headers;
        foreach ($headers as $header) {
            if (strstr($header, "Location")) {
                echo $uri = str_replace("Location:", "", $header);
                break;
            }
        }
    } else {
        echo "Upload faild";
    }

    $cnt = 0;
    $chunk_size = 256 * 1024 * 1;
    $handle = fopen($videoPath, 'rb');
    if ($handle === false) {
        return false;
    }
    $progress = 0;
    while (!feof($handle)) {
        $buffer = fread($handle, $chunk_size);
        $cnt = strlen($buffer);
        $lastBytePos = $progress + $cnt - 1;
        $curl = new Curl();
        $curl->setHeader("Host", "picasaweb.google.com");
        $curl->setHeader("Content-Length", $chunk_size);
        $curl->setHeader("Authorization", "Bearer $access_token");
        $curl->setHeader("Content-Type", $mimeType);
        $curl->setHeader("User-Agent", "CXchange-CXchange-1.0 cURL/1.11.3");
        $curl->setHeader("Accept-encoding", "identity");
        $curl->setHeader("Content-Range", "bytes $progress-$lastBytePos/$content_length");
        $curl->setHeader("GData-Version", "2");
        $curl->setHeader("MIME-version", "1.0");
        $curl->setHeader("Expect", "");

        $responce = $curl->put(trim($uri), $buffer);
        $responce_code = $curl->http_status_code;
        if ($responce_code == 308) {
            $headers = $curl->response_headers;
            foreach ($headers as $header) {
                if (strstr($header, "Range:")) {
                    $temp = explode("-", $header);
                    $progress = end($temp)+1;
                    break;
                }
            }
        } else {
            echo "Upload faild";
        }
    }
    fclose($handle);
} catch (Exception $e) {
    echo '';
}

【问题讨论】:

  • 您是否尝试过发送另一个文件(可能是较小的文件)?

标签: oauth-2.0 google-drive-api picasa google-data-api zend-gdata


【解决方案1】:

代码:400 msg:文件过早结束。

读完这个话题,I found the solution 是给第一个会话请求一些东西。你不能离开它没有身体。

$xmldata =
'<entry xmlns="http://www.w3.org/2005/Atom">
    <title>cats.jpg</title>
    <summary>Real cat wants attention too.</summary>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/photos/2007#photo"/>
 </entry>';
$request  = new Google_Http_Request(
    "https://picasaweb.google.com/data/upload/resumable/media/create-session/feed/api/user/{$person->id}/albumid/6185252512807301377?alt=json&access_token=" .$token['access_token'],
    "POST",
    array('GData-Version' => 2,
        'Content-Type' => 'application/atom+xml',
        'MIME-Version' => '1.0',
        'Content-Length' => strlen($xmldata),
        'X-Upload-Content-Length' => $size,
        'X-Upload-Content-Type' => 'image/jpeg',
        'Slug' => 'xxxx.jpg',
        'Authorization' => "Bearer {$token['access_token']}"),
        $xmldata
);
$response = Google_Http_REST::doExecuteRaw($client, $request);

Slug 标头是必需的,但文件名将是 cats.jpg (xml)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多