Php 在Slim中通过URL和post请求传递数据
我正在创建一个API,我想知道是否有任何方法可以通过URL通过post方法传递数据Php 在Slim中通过URL和post请求传递数据,php,rest,spring-boot,api,slim,Php,Rest,Spring Boot,Api,Slim,我正在创建一个API,我想知道是否有任何方法可以通过URL通过post方法传递数据 add?name={name}&age={age}… 我没有太多的API开发经验,但我记得这个URL是在SpringBoot中工作的 提前感谢。您可以像在任何类型的HTTP请求中那样在URL中传递数据。 也就是说,在使用POST/PUT/PATCH时,我建议使用JSON负载作为请求主体发送 如果你想读斯利姆的论点 <?php $app->get('/hello/{name}', functi
add?name={name}&age={age}…
我没有太多的API开发经验,但我记得这个URL是在SpringBoot中工作的
提前感谢。您可以像在任何类型的HTTP请求中那样在URL中传递数据。 也就是说,在使用POST/PUT/PATCH时,我建议使用JSON负载作为请求主体发送 如果你想读斯利姆的论点
<?php
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
// or
$name = $request->getArgument('name');
// When Request URL is
// /hello/world?age=24
// you can read the age argument as
$name = $request->getArgument('age');
// This behaviour is defined in PSR-7 and available across PHP frameworks
return $response;
});
您可以像在任何类型的HTTP请求中一样在URL中传递数据。
也就是说,在使用POST/PUT/PATCH时,我建议使用JSON负载作为请求主体发送
如果你想读斯利姆的论点
<?php
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
// or
$name = $request->getArgument('name');
// When Request URL is
// /hello/world?age=24
// you can read the age argument as
$name = $request->getArgument('age');
// This behaviour is defined in PSR-7 and available across PHP frameworks
return $response;
});
首先,创建一个不带任何查询参数的路由来处理POST HTTP请求
$app->post('/add', function (Request $request, Response $response, array $args) {
// get query parameters as an associative array
$params = $request->getQueryParams();
// return $params as a JSON data
$response->getBody()->write(json_encode($params));
return $response->withHeader('Content-Type', 'application/json');
});
然后,使用查询参数向该路由发出POST HTTP请求:/add?name=Foo&age=27
输出:
{
"name": "Foo",
"age": "27"
}
首先,创建一个无需任何查询参数即可处理POST HTTP请求的路由
$app->post('/add', function (Request $request, Response $response, array $args) {
// get query parameters as an associative array
$params = $request->getQueryParams();
// return $params as a JSON data
$response->getBody()->write(json_encode($params));
return $response->withHeader('Content-Type', 'application/json');
});
然后,使用查询参数向该路由发出POST HTTP请求:/add?name=Foo&age=27
输出:
{
"name": "Foo",
"age": "27"
}