无法从PUT请求中收集正文并将其发送到数据库

时间:2019-05-30 09:32:32

标签: php http postman put

我尝试在php中创建一个rest api,并且对PUT请求有问题。我用

parse_str(file_get_contents("php://input"), $post_vars);
    $marca = $post_vars["marca"];
    $link = $post_vars["link"];

并确认此错误:

Notice: Undefined index: marca in 
D:\PHP\htdocs\ExLab3.2\application\Controller\BecuriController.php on line 64

Notice: Undefined index: link in 
D:\PHP\htdocs\ExLab3.2\application\Controller\BecuriController.php on line 65

使用邮递员发送请求。 我设法在屏幕$ post_vars上打印并具有以下形式:

Array ( [----------------------------622495270320150137483080 Content-    Disposition:_form-data;_name] => "marca" new mark ----------------------------622495270320150137483080 Content-Disposition: form-data; name="capacitate" 12 ----------------------------622495270320150137483080 Content-Disposition: form-data; name="link" new link ----------------------------622495270320150137483080 Content-Disposition: form-data; name="bec_id" 2 ----------------------------622495270320150137483080-- )

我无法编码成json并放入变量中。

$id = $post_vars["bec_id"];

这是我处理换手请求的功能:

public function handle_put()
{
    parse_str(file_get_contents("php://input"), $post_vars);
    $marca = $post_vars["marca"];
    $link = $post_vars["link"];

    print_r($post_vars);
    if (!empty($_POST["marca"]) && !empty($_POST["capacitate"]) && !empty($_POST["link"]) && !empty($_POST["bec_id"]) && ctype_alpha($_POST["marca"]) && ctype_digit($_POST["capacitate"]) && ctype_alpha($_POST["link"])) {
        $bec_model = new BecuriModel();
        $bec_model->update_bec(
            $_POST["marca"],
            $_POST["capacitate"],
            $_POST["link"],
            $_POST['bec_id']
        );
        $response = array(
            'id'     => htmlentities($_POST["bec_id"]),
            'marca' => htmlentities($_POST["marca"]),
            'capacitate'  => htmlentities($_POST["capacitate"]),
            'link'   => htmlentities($_POST["link"])
        );
        return $response;
    } else {
        http_response_code(400);
        echo json_encode(array("message" => "Eroare."));
    }
}

enter image description here

0 个答案:

没有答案