php - Doing a post request via url in Slim Framework

614

I'm trying to make apostrequest in slim, my intention is inserting data into a mysql database. It's the first time I try to do this, so sorry if I don't explain myself well.

Here's what I have:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require 'vendor/autoload.php';
require "classes/Autoloader.php";

$app = new \Slim\App;

$app->post('/', function(Request $request, Response $response) use ($app) {

    $postVars = $request->getParsedBody();
    $id = $request->getAttribute('id');
    $steps = $request->getAttribute('steps');
    $date = $request->getAttribute('date');

    echo $id . $steps . $date;

    require_once "classes/Connection.php";
    $userdata = new Insert($dbh, $id, $steps, $date);
    $userdata->insert();
});
$app->run();

My intention is getting the values, and using them to insert the data, but I keep getting Slim's "Page Not Found" error. This is the url I'm trying with:http://localhost/wp-api/?id=1&steps=12&date=8787.

What am I doing wrong, or is this the correct way to do it?

Thanks in advance!

Edit: Following Justal's answer, I changed my code, on line 10;$app->post('/'... specifically. I now getMethod not allowed. Must be one of: POST

Edit2: I changed line 12 (getQueryParams ->getParsedBody) , and used Postman, otherwise broswer does agetrequest (source of the previous error). It now insertsnull values into the database, though.

848

Answer

Solution:

page not found because you are requesting get on a post route.try below code.

All Data pass in the $request object.

$app->post('/', function(Request $request, Response $response) {

echo "<pre>";
print_r($request);
exit;    

});

People are also looking for solutions to the problem: javascript - how to set highchart x-axis to display more dates and change date format

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.