php - 使用 PHP 和 fsockopen() 将文件上传到 Google 代码托管

标签 php post fsockopen

Google Code Hosting 可以远程上传文件。我一直在尝试用 PHP 编写一个脚本,将文件上传到我的帐户。这是脚本本身:

<?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;

我面临的问题是服务器响应“411 Length Required”。但是,我的 header 中确实有 Content-Length。那么,为什么这不起作用?

这是 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--

我不明白。

最佳答案

HTTP 标准要求您指定您使用的 HTTP 协议(protocol)版本。在您的情况下,将 header 更改为:

$headers = "POST $projectUri HTTP/1.0
Host: $project.googlecode.com
Authorization: Basic $authToken
Content-Type: multipart/form-data; boundary=$boundary
Content-Length: $bodyLength

";

我认为如果您不指定,Google 会假设您使用的是协议(protocol)的旧版本 (<1.0)。

关于php - 使用 PHP 和 fsockopen() 将文件上传到 Google 代码托管,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1733943/

相关文章:

php - 正在发送 POST header ,但未在服务器上接收到

php - Paypal IPN 响应适用于 Paypal 但不适用于 sandbox-Paypal?

php - 用于 paypal ipn 的 curl 或 fsockopen

node.js - 在 NodeJS 中发送不同的 POST 响应

codeigniter - Nginx & PHP5-fpm + CodeIgniter 或 Kohana = 404 错误?

PHP PDO 扩展不可用是命令行

php - 找不到错误:/

javascript - 使用带有ajax的post请求将html表单数据发送到php到https ://foo. com

php - fsockopen 在端口 25 和 587 上被阻止

php - AWS Beanstalk 文档目录