Google Code Hosting has the ability to upload files to it remotely. I've been trying to program a script in PHP which uploads files to my account. Here's the script itself:
<?php
/* I censored a few details. */
$username = '***@gmail.com';
$password = '***';
$file = 'test.txt';
$project = '***';
$summary = 'test';
$uploadHost = "$project.googlecode.com";
$projectUri = '/files';
$authToken = base64_encode("$username:$password");
define('CRLF',"\r\n");
$boundary = "afdljgdfgjidofjgoidjfgoijdfogjsog9054uy54rhh";
$fileContents = quoted_printable_encode(file_get_contents($file));
$body = array();
/* Summary field is required. */
$body[] = '--' . $boundary . CRLF . 'Content-Disposition: form-data; name="summary"' . CRLF . 'Content-Length: 4' . CRLF . CRLF . 'test';
/* The actual file. */
$body[] = '--' . $boundary . CRLF . 'Content-Disposition: form-data; name="filename"; filename="' . $file . '"' . CRLF .'Content-Type: text/plain' . CRLF . 'Content-Length: ' . strlen($fileContents) . CRLF . 'Content-Type: application/octet-stream' . CRLF . CRLF . $fileContents;
/* The end. */
$body[] = '--' . $boundary . '--';
$bodyString = implode(CRLF, $body);
$bodyLength = strlen($bodyString);
$headers = "POST $projectUri
Host: $project.googlecode.com
Authorization: Basic $authToken
Content-Type: multipart/form-data; boundary=$boundary
Content-Length: $bodyLength
";
$fp = fsockopen("ssl://$uploadHost", 443, $errno, $errstr);
if ($fp)
{
fwrite($fp, $headers . $bodyString);
$response = '';
while (!feof($fp))
$response .= fgets($fp, 1024);
fclose($fp);
}
/* For debugging. */
header('Content-Type: text/plain'开发者_如何学运维);
echo $headers.$bodyString;
echo CRLF . CRLF . CRLF . $response;
The problem I'm facing is that the server responds with "411 Length Required". However, I do have Content-Length within my headers. So, why does this not work?
Here's the actual request in HTTP:
POST /files
Host: ***.googlecode.com
Authorization: Basic ***
Content-Type: multipart/form-data; boundary=afdljgdfgjidofjgoidjfgoijdfogjsog9054uy54rhh
Content-Length: 386
--afdljgdfgjidofjgoidjfgoijdfogjsog9054uy54rhh
Content-Disposition: form-data; name="summary"
Content-Length: 4
test
--afdljgdfgjidofjgoidjfgoijdfogjsog9054uy54rhh
Content-Disposition: form-data; name="filename"; filename="test.txt"
Content-Type: text/plain
Content-Length: 8
Content-Type: application/octet-stream
testtext
--afdljgdfgjidofjgoidjfgoijdfogjsog9054uy54rhh--
I don't understand.
The HTTP standard requires you to specify which HTTP protocol version you are using. In your case, change the headers to:
$headers = "POST $projectUri HTTP/1.0
Host: $project.googlecode.com
Authorization: Basic $authToken
Content-Type: multipart/form-data; boundary=$boundary
Content-Length: $bodyLength
";
I think Google is assuming you are using an older version (<1.0) of the protocol if you don't specify it.
Try this:
http://code.google.com/p/send2goog/source/browse/trunk/send2goog
精彩评论