[英]Doing a post request via url in Slim Framework
I'm trying to make a post
request in slim, my intention is inserting data into a mysql database. 我试图发出苗条的
post
请求,我的目的是将数据插入mysql数据库。 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. 我的目的是获取值,并使用它们来插入数据,但是我一直收到Slim的“找不到页面”错误。 This is the url I'm trying with:
http://localhost/wp-api/?id=1&steps=12&date=8787
. 这是我尝试使用的网址:
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; 编辑:按照Justal的回答,我在第10行更改了代码;
$app->post('/'...
specifically. I now get Method not allowed. Must be one of: POST
$app->post('/'...
专门。我现在得到Method not allowed. Must be one of: POST
Edit2: I changed line 12 ( getQueryParams
-> getParsedBody
) , and used Postman, otherwise broswer does a get
request (source of the previous error). Edit2:我更改了第12行(
getQueryParams
> getParsedBody
),并使用了Postman,否则broswer会执行get
请求(先前错误的来源)。 It now inserts null
values into the database, though. 现在,它现在将
null
值插入数据库。
page not found because you are requesting get on a post route.try below code. 找不到页面,因为您正在请求张贴路线。请尝试以下代码。
All Data pass in the $request object. 所有数据都传递到$ request对象中。
$app->post('/', function(Request $request, Response $response) {
echo "<pre>";
print_r($request);
exit;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.