PHP 无法获取作为多部分/表单数据发送的原始数据

本文关键字:数据 表单 原始数据 获取 多部 PHP | 更新日期: 2023-09-27 17:56:27

这是客户端代码(C#):

string boundary = "--ABC";
try
{
    WebRequest request = WebRequest.Create(url);
    request.Method = "POST";                
    request.ContentType = "multipart/form-data;boundary="+boundary;

    using (var requestStream = request.GetRequestStream())
    using (var writer = new StreamWriter(requestStream))
    {
        writer.WriteLine(boundary);
        writer.WriteLine("Content-Disposition: form-data; name='"data'"");
        writer.WriteLine();
        writer.WriteLine("abcdefg");
        writer.WriteLine(boundary + "--");
        writer.Flush();
    }
    string responseData = string.Empty;
    using (var response = request.GetResponse())
    using (var responseStream = response.GetResponseStream())
    using (var reader = new StreamReader(responseStream))
    {
        responseData=reader.ReadToEnd();
    }

这是服务器代码 (PHP):

print file_get_contents("php://input");

或:

print $_POST["data"];

或:

print $http_raw_post_data;

或:

$fp=fopen("php://output","rb");
$contents=fread($fp,5);
fclose($fp);
print $contents;

这些代码都不起作用,全部打印为空。
有人可以帮忙吗?

PHP 无法获取作为多部分/表单数据发送的原始数据

以下示例说明了"多部分/表单数据"编码。假设我们有以下形式:

 <FORM action="http://server.com/cgi/handle"
       enctype="multipart/form-data"
       method="post">
   <P>
   What is your name? <INPUT type="text" name="submit-name"><BR>
   What files are you sending? <INPUT type="file" name="files"><BR>
   <INPUT type="submit" value="Send"> <INPUT type="reset">
 </FORM>

如果用户在文本输入中输入"Larry",并选择文本文件"file1.txt",则用户代理可能会发回以下数据:

Content-Type: multipart/form-data; boundary=AaB03x
   --AaB03x
   Content-Disposition: form-data; name="submit-name"
   Larry
   --AaB03x
   Content-Disposition: form-data; name="files"; filename="file1.txt"
   Content-Type: text/plain
   ... contents of file1.txt ...
   --AaB03x--

如果用户选择了第二个(图像)文件"file2.gif",则用户代理可能会按如下方式构造部件:

   Content-Type: multipart/form-data; boundary=AaB03x
   --AaB03x
   Content-Disposition: form-data; name="submit-name"
   Larry
   --AaB03x
   Content-Disposition: form-data; name="files"
   Content-Type: multipart/mixed; boundary=BbC04y
   --BbC04y
   Content-Disposition: file; filename="file1.txt"
   Content-Type: text/plain
   ... contents of file1.txt ...
   --BbC04y
   Content-Disposition: file; filename="file2.gif"
   Content-Type: image/gif
   Content-Transfer-Encoding: binary
   ...contents of file2.gif...
   --BbC04y--
   --AaB03x--

C# 中的多部分表单发布示例

试试这个:

 string boundary = "AaB03x";
    try
    {
        WebRequest request = WebRequest.Create(url);
        request.Method = "POST";                
        request.ContentType = "multipart/form-data;boundary="+boundary;
        using (var requestStream = request.GetRequestStream())
        using (var writer = new StreamWriter(requestStream))
        {
            writer.WriteLine("--"+boundary);
            writer.WriteLine( "Content-Disposition: form-data; name='"files'"; filename='"file1.txt'"");
             writer.WriteLine("Content-Type: text/plain
    ");
            writer.WriteLine("example");        
            writer.WriteLine("--"boundary + "--");
            writer.Flush();
        }
        string responseData = string.Empty;
        using (var response = request.GetResponse())
        using (var responseStream = response.GetResponseStream())
        using (var reader = new StreamReader(responseStream))
        {
            responseData=reader.ReadToEnd();
        }