原生PHP接收$_POST的几种方式

  • > HTTP 常见 Content-Type

    > application/x-www-form-urlencoded
    > multipart/form-data
    > application/json

  • > $_POST 默认只能接收到 Content-Type: application/x-www-form-urlencoded 的数据

  • > 如果Content-Type: application/json 需要用到php://input 处理输入流

    > 请求内容 {"account": "123456"}
    > $tmpData = strval(file_get_contents("php://input"));
    > $DataArray = json_decode($tmpData, true);
    > $account = $DataArray[‘account‘];

  • > Content-Type: multipart/form-data

    > $tmpData = strval(file_get_contents("php://input"));
    > public function parseData($data) {
    > $list = explode("\r\n", $data);
    > foreach($list as $value) {
    > if($value) {
    > if(strstr($value, ‘--‘)) continue;
    > if(strpos($value, ‘-‘)) {
    > $key = str_replace(‘"‘, ‘‘, strchr($value, ‘"‘));
    > continue;
    > };
    > if($value) {
    > $array[$key] = $value;
    > }
    > }
    > }
    > return $array;
    > }
    > $DataArray = $this->parseData($tmpData);
    > $DataArray[‘account‘];

  • 相关文章