通过带有Slim的发布请求的URL传递数据

问题描述 投票:0回答:2

我正在创建一个API,我想知道是否有任何方法可以通过带有post方法的URL传递数据,例如

add?name={name}&age={age}…

我在API开发方面没有太多经验,但是我记得这个URL在Spring Boot中确实有用。

提前感谢。

php rest spring-boot api slim
2个回答
0
投票

您可以像在任何类型的HTTP请求中一样在URL中传递数据。也就是说,在使用POST / PUT / PATCH时,我建议使用作为请求正文发送的JSON有效负载。

如果您想以Slim的方式读取参数,则>

<?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;
});

在普通的PHP中,您可以使用超全局性$ _GET或$ _POST读取参数

<?php
$name = $_GET['age'];

0
投票

首先,创建一个处理POST HTTP请求而没有任何查询参数的路由。

© www.soinside.com 2019 - 2024. All rights reserved.