Slim JSON Outputs(苗条的 JSON 输出)
问题描述
我正在使用 Slim 框架和 PHP 来为我的应用程序创建一个 RESTful API.然而,我假设框架将有一些方法来创建更简单的 JSON 输出,而不仅仅是 exit($jsonEncodedVariable);.
I am using the Slim framework with PHP to create a RESTful API for my app. However, I assumed that the framework would have some way of creating easier JSON outputs rather than just exit($jsonEncodedVariable);.
我是否在框架中遗漏了某些内容,或者我是否需要对每个方法都使用 json_encode()... exit($json)...?
Am I missing something in the framework, or do I need to use json_encode()... exit($json)... for every method?
所有数据都从我的 MySQL 数据库中取出,然后根据调用的 REST 请求放入 JSON 数组中.
All of the data is taken out of the my MySQL database and would then be put into a JSON array depending on what REST request was called.
例如,如果 /api/posts/all 被请求,我将 exit() 一个 JSON 数组,其中包含所有帖子的每个值对应其自己的键,值":键.
For example, if /api/posts/all was requested, I would exit() a JSON array of all the posts which each value for its own key, "value" : key.
我的问题是,有没有一种简单的方法,使用 slim 框架,exit()'ing JSON 代码而不是作为纯文本退出?
My question is, is there an easy way, using the slim framework, for exit()'ing JSON code instead of exiting it as plain text?
推荐答案
header("Content-Type: application/json");
echo json_encode($result);
exit;
提示:使用 Slim用于开发 REST API 的 PHP 框架
这篇关于苗条的 JSON 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:苗条的 JSON 输出
基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
