简体   繁体   中英

Decoding x-www-form-urlencoded to array

I'm trying to transform PUT request params to a key=>value array in my server.

parse_str(file_get_contents('php://input'), $data);

This way I get a "dirt" array like this:

Array
(
    [------WebKitFormBoundaryXXXXXXXXXXXXXXXX
Content-Disposition:_form-data;_name] => "birth_date"

1995-01-03
------WebKitFormBoundaryXXXXXXXX
Content-Disposition: form-data; name="age"

15
------WebKitFormBoundaryXXXXXXXX--

)

How can I extrapolate just the two key=>values without manually parsing the content...?

The request's body params were encoded in multipart/form-data and not application/x-www-form-urlencoded .

The x-www-form-urlencoded version would look like this:

birth_date=1995-01-03&age=15

Parsing multipart/form-data is a little bit more advanced. As far as I know there is no built-in function in PHP to do that. But there are third party libraries that you may use: https://github.com/h4cc/multipart

If you are able to chenge the way the request is encoded it would be much easier and less error prone to change the encoding to x-www-form-urlencoded .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM