Picasa图片/视频可恢复上传问题

时间:2015-06-27 05:58:20

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

我正在尝试使用google api resumable上传选项上传视频/图片。

如果我有200MB的文件那么我必须上传块次数N次。 我的代码返回308状态代码N-1次(除了最后一个)。对于最后一块谷歌谷歌没有回应我(我的PHP服务器说"空回复服务器")

请帮助我,我错了。

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 '';
}

1 个答案:

答案 0 :(得分:0)

代码: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)。