I'm trying to rebuild my PHP API with SlimPHP v3 + PDO. The problem is I'm stuck with retrieving attributes passed in POST method. Here is part of my index.php file, I decided just to echo the variable to test if it's working.
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require_once '../includes/UserOperation.php';
require_once '../includes/TweetOperation.php';
$app = new \Slim\App([
'settings' => [
'displayErrorDetails' => true
]
]);
//---UserOperation.php---//
//registering a new user
$app->post('/register', function (Request $request, Response $response) {
if (isTheseParametersAvailable(array('name', 'email', 'password', 'picture_path'))) {
$requestData = $request->getParsedBody();
$name = $request->getParsedBody()['name'];
$email = $requestData['email'];
$password = $requestData['password'];
$picture_path = $requestData['picture_path'];
echo "Hello " .$name;
}
});
//function to check parameters
function isTheseParametersAvailable($required_fields)
{
$error = false;
$error_fields = "";
$request_params = $_REQUEST;
foreach ($required_fields as $field) {
if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
$error = true;
$error_fields .= $field . ', ';
}
}
if ($error) {
$response = array();
$response["error"] = true;
$response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
echo json_encode($response);
return false;
}
return true;
}
$app->run();
The link for my website looks for example like below,but $name variable that I'm trying to echo remains empty.
https://myweblink/register?name=test&email=test@mail.com&password=pass&picture_path=lolololo