如何使用PHP发送multipart请求

时间:2016-06-15 09:58:38

标签: php ajax wordpress curl

我正在我的注册页面上传图片,该页面由ajax提交并发送到ajaxcall.php,在那里我向请求添加一些标头并将其发送到注册用户的注册api

问题:我在ajaxcall.php中收到$ _POST和$ _FILE但是在发送带有相同参数的curl请求时添加了几个标题我没有收到服务器端的post params

以下是创建内容存储的代码

 function genereatePostFileVars($file,$rem=array(),$boundary)
{
    $postvar="";

    foreach($_POST as $key=>$val)
    {
        if($key!='endpoint')
        {
            if(!in_array($key,$rem))
            {
                if($key != 'uname')
                {
                $postvar.="\r\n$boundary\r\nContent-Disposition: form-data; name=\"$key\"\r\n\r\n$val";
                }
                else
                {
                    $postvar.="\r\n$boundary\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\n$val";
                }
            }
        }

    }
    if(!empty($_FILES))
    {
        $filedata=array();
        if(count($_FILES[$file]['name'])>1)
        {
            for($i=0;$i<count($_FILES[$file]['name']);$i++)
            {
                $fst='file'.($i+1);
                $filedata[$fst]['name']=$_FILES[$file]['name'][$i];
                $filedata[$fst]['error']=$_FILES[$file]['error'][$i];
                $filedata[$fst]['tmp_name']=$_FILES[$file]['tmp_name'][$i];
                $filedata[$fst]['size']=$_FILES[$file]['size'][$i];
            }
        }
        else{
            $filedata['file']=$_FILES[$file];
;
        }
        foreach($filedata as $name=>$value){

            $postvar.="\r\n".$boundary."\r\nContent-Disposition: form-data; name=\"$name\"; filename=\"".$value['name']."\"\r\nContent-Type:".$value['type']."\r\n";
        }
    }
    $postvar.="\r\n$boundary--";
    return $postvar;
}

上述代码的输出:

-----1465984203238231624
Content-Disposition: form-data; name="name"

himanshi
-----1465984203238231624
Content-Disposition: form-data; name="username"

wvhxc
-----1465984203238231624
Content-Disposition: form-data; name="email"

wvhxc@gmail.com
-----1465984203238231624
Content-Disposition: form-data; name="password"

vdschh
-----1465984203238231624
Content-Disposition: form-data; name="file"; filename="10940545_822721561108202_6765172210735079451_n.jpg"
Content-Type:image/jpeg

-----1465984203238231624--

发送的标题:

Array
(
    [0] => Accept:application/json,image/*
    [1] => Expect: 
    [2] => authToken: 72b5a44d3e9c50fc81dd196d75a1b375
    [3] => timestamp: 2016-06-15 09:50:03
    [4] => content-type: multipart/form-data; boundary=-----1465984203238231624
    [5] => deviceType:Web
    [6] => apiVersion:v0.1
)

我得到的回应:

HTTP/1.1 400 Bad Request
Date: Wed, 15 Jun 2016 09:50:03 GMT
Server: Apache/2.2.29 (Unix) mod_wsgi/3.5 Python/2.7.10 PHP/5.6.10 mod_ssl/2.2.29 OpenSSL/0.9.8zh DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.9 Perl/v5.22.0
X-Powered-By: PHP/5.6.10
Set-Cookie: PHPSESSID=5f784328aa7571352c1ba2e127a96ef6; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-Content-Type-Options: nosniff
Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
Access-Control-Allow-Headers: Authorization
Allow: POST
Content-Length: 80
Connection: close
Content-Type: application/json; charset=UTF-8

{"status":false,"message":"Username or email is required for signup.","data":[]}

请让我知道我做错了什么,提前致谢

0 个答案:

没有答案