I am trying to upload a video/image by using google api resumable upload option .
If i have 200MB file So i have to do chunk upload N times . My code return 308 status code for N-1 times (Except last one). For last chunk google not responding me (My php server says "empty-reply-from-server")
Please help me where i am wrong.
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 '';
}
code: 400 msg: Premature end of file.
After read this topic, I found the solution is to give something to first session request. You just can't leave it without a body.
Slug header is required but file name will be
cats.jpg
(xml).