I try to create a rest api in php and have problem with PUT request. I use
parse_str(file_get_contents("php://input"), $post_vars);
$marca = $post_vars["marca"];
$link = $post_vars["link"];
and receve this error:
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
Use postman for send request. I managed to print on screen $post_vars and have this form:
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-- )
I cant manage to encode into json for put into a variable.
$id = $post_vars["bec_id"];
This is my function for handre put request:
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."));
}
}
