php上传报文,php 文件上传,这段报文哪里错了呢

看了很多例子 没解决问题,大家帮我看看呢

返回的结果如下:

HTTP/1.1 400 Bad Request

Date: Fri, 18 Jul 2014 18:15:46 GMT

Server: nginx/1.4.4

Content-Type: application/json

Content-Length: 36

Access-Control-Allow-Headers: X-File-Name, X-File-Type, X-File-Size

Access-Control-Allow-Methods: OPTIONS, HEAD, POST

Access-Control-Allow-Origin: *

Cache-Control: no-store, no-cache, must-revalidate

Pragma: no-cache

X-Content-Type-Options: nosniff

X-Log: UP/400

X-Reqid: QkEAAC2bX1liE4IT

X-Via: 1.1 js41:3 (Cdn Cache Server V2.0)

Connection: close

代码:

在这里生成的token

http://jsfiddle.net/gh/get/extjs/4.2/icattlecoder/jsfiddle/tree/master/uptoken

在这里测试上传成功

http://jsfiddle.net/gh/get/library/pure/icattlecoder/jsfiddle/tree/master/formupload

回复内容:

看了很多例子 没解决问题,大家帮我看看呢

返回的结果如下:

HTTP/1.1 400 Bad Request

Date: Fri, 18 Jul 2014 18:15:46 GMT

Server: nginx/1.4.4

Content-Type: application/json

Content-Length: 36

Access-Control-Allow-Headers: X-File-Name, X-File-Type, X-File-Size

Access-Control-Allow-Methods: OPTIONS, HEAD, POST

Access-Control-Allow-Origin: *

Cache-Control: no-store, no-cache, must-revalidate

Pragma: no-cache

X-Content-Type-Options: nosniff

X-Log: UP/400

X-Reqid: QkEAAC2bX1liE4IT

X-Via: 1.1 js41:3 (Cdn Cache Server V2.0)

Connection: close

代码:

在这里生成的token

http://jsfiddle.net/gh/get/extjs/4.2/icattlecoder/jsfiddle/tree/master/uptoken

在这里测试上传成功

http://jsfiddle.net/gh/get/library/pure/icattlecoder/jsfiddle/tree/master/formupload

$head.="Content-Length: ".strlen($data)."\r\n\r\n";

注意 Content-Length: 后面需要有一个空格。

大神,你用这么多header干啥呢??完全看不懂代码、、

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您输入的是一个 POST 请求报文,您可以尝试使用以下 PHP 代码将其转换为 Python requests 格式: ```php <?php if($_SERVER['REQUEST_METHOD'] == 'POST') { $request = $_POST['request']; // 解析请求报文 $lines = explode("\r\n", $request); $method = ''; $url = ''; $headers = []; $data = ''; foreach($lines as $line) { if(empty($method)) { $request_line = explode(' ', $line); $method = $request_line[0]; $url = $request_line[1]; } else { if($line === '') { // 请求头和请求体数据之间有一个空行 $data = implode("\r\n", array_slice($lines, array_search('', $lines) + 1)); break; } else { // 解析请求头 $header = explode(': ', $line); $headers[] = [ 'name' => $header[0], 'value' => $header[1] ]; } } } // 将请求报文转换为 Python requests 格式 $python_code = 'import requests' . PHP_EOL; $python_code .= 'url = "' . $url . '"' . PHP_EOL; $python_code .= 'headers = {' . PHP_EOL; foreach($headers as $header) { $python_code .= ' "' . $header['name'] . '": "' . $header['value'] . '",' . PHP_EOL; } $python_code .= '}' . PHP_EOL; if($method == 'GET') { $python_code .= 'response = requests.get(url, headers=headers)' . PHP_EOL; } else { $python_code .= 'data = ' . var_export($data, true) . PHP_EOL; $python_code .= 'response = requests.post(url, headers=headers, data=data)' . PHP_EOL; } $python_code .= 'print(response.content)' . PHP_EOL; // 输出转换后的 Python 代码 echo '<pre>' . htmlspecialchars($python_code) . '</pre>'; } ?> <form method="post"> <label>POST 请求报文:</label><br> <textarea name="request" rows="10" cols="50"></textarea><br> <input type="submit" value="提交"> </form> ``` 这段代码会在页面上显示一个输入框,您可以在其中输入 POST 请求报文。当您点击提交按钮时,它会将请求报文转换为 Python requests 格式,并把转换后的 Python 代码输出到页面上。请注意,这个代码只是一个简单的示例,您需要根据您实际的需求进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值