du13932014807 2019-05-03 16:59
浏览 228
已采纳

无法从php中的任何HTTP请求中获取params

I can't get request params from php//input, it's always returning empty string. $_POST is also always empty, the only way I could get params is using $_REQUEST, and I'm not sure if it's right way to solve this problem.

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

returns: string(0) ""

var_dump($_POST);

returns: array(0) { }

var_dump($_REQUEST);

returns: array(2) { ["ergerg"]=> string(6) "ergerg" ["regergerg"]=> string(6) "ergreg" }

My request in Postman looks like :

POST  /api/ajax.php?ergerg=ergerg&regergerg=ergreg

response
Status:200 OK
Time:192 ms
Size:236 B

ajax.php

<?php
include "config.php";

$rest_json = file_get_contents("php://input");
$obj = json_decode($rest_json);
$request = $obj->request;

if($request == 1){
    $userData = mysqli_query($con, "select * from rows");

    $response = array();

    while ($row = mysqli_fetch_assoc($userData)) {
        $response[] = $row;
    }
    echo json_encode($response);
}

if($request == 2){
    $platform   = $data->platform;
    $container1 = $data->container1;
    $container2 = $data->container2;
    $ZPU1       = $data->ZPU1;
    $ZPU2       = $data->ZPU2;
    $reportId   = $data->reportId;
    mysqli_query($con, "INSERT INTO 
                            rows(platform,container1,container2,
                                ZPU1,ZPU2,reportId) 
                    VALUES('" . $platform . "','" . $container1 . "','" . 
                            $container2 . "','" . $ZPU1 . "','" . $ZPU2 . 
                            "','" . $reportId . "')");
    echo "Insert successfully";
}

exit;

in config file there is just connection to mysql database :

config.php

<?php

$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = "password"; /* Password */
$dbname = "dbname"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
  die("Connection failed: " . mysqli_connect_error());
}
  • 写回答

1条回答 默认 最新

  • douhan9619 2019-05-04 10:31
    关注

    I think you may be confused about how POST requests and HTTP bodies work. Apologies if this is actually just a mistake in your question.

    An HTTP request can include several key parts:

    • The request method - GET, POST, PUT, DELETE, etc
    • The URL requested
    • Headers
    • A "body", containing additional data

    If you submit a plain HTML form from a website, it will either be turned into a GET request, with the parameters encoded into the URL, or a POST request, with the parameters encoded into the request body. That's why PHP has arrays called $_GET and $_POST, for retrieving the form details in the two cases - but they don't actually correspond to the HTTP methods, just the way the data is encoded.

    php://input represents the request body, regardless of what HTTP method was used.

    So, finally, to your example request: you have specified a URL, with some parameters encoded in it; PHP will decode those into $_GET; you have specified an HTTP method of POST; but you haven't sent anything in the request body, so php://input will be empty.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大