提问人:Jarosław Bielawski 提问时间:8/7/2023 更新时间:8/9/2023 访问量:70
Fat-Free Framework 如何获取 post api 请求?
How does Fat-Free Framework get post api request?
问:
我想在 Fat-Free Framework 上创建一个 API POST 处理程序。
当我在应用程序内发送任何测试 POST 请求时,没有任何反应,找不到错误的原因。
发送请求
$dataContainers = [
'type' => 1,
'units' => 1,
'container' => 1,
];
$query = http_build_query($data);
$options = ['http' => [
'method' => 'POST',
'header' => ['Content-Type: application/json; charset=utf-8'],
'content' => $query,
],
];
$context = stream_context_create($options);
$response = @file_get_contents('/api/packer/containers', false, $context);
路由.ini
POST /api/packer/containers = App\Controller\Packer\PackerApiController->setContainers
控制器
class PackerApiController
{
private $data;
public function __construct($f3, $params)
{
// Trying to test get and return any data
$params; // empty
$post = json_decode(file_get_contents('php://input'), true); // empty
$post = json_decode($f3->get('BODY'), true); // empty
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-Type: application/json; charset=utf-8');
return json_encode($post);
}
}
答:
1赞
george007
8/9/2023
#1
您定义了指向 的方法的路由。但是,您尝试在此类的构造函数中处理请求。创建方法并将代码移动到该方法。setContainers
PackerApiController
评论
@
@file_get_contents
$response =
var_dump($response);
file_get_contents
http://mytestsystem.local/api/packer/containers