PHP 中的 REST API 输出到 JSON 文件扩展名 [英] REST API in PHP output to JSON file extension

查看:14
本文介绍了PHP 中的 REST API 输出到 JSON 文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想用 PHP 编写一个用于 JSON 的 REST API,以便在 iPhone 上使用,但也可以在很多网站和设备上使用.我有以下代码,当通过 GET 语句访问时,返回一个文件,如:

So I want to write a REST API in PHP for JSON for consumption on the iPhone, but also a lot of websites and devices. I have the below code, when accessed via a GET statement, returns a file like:

1mdi2o3.part

1mdi2o3.part

我如何返回类似:users.json

How do I return something like: users.json

$db->setQuery( "SELECT * FROM users");
$db->query() or die($queryError);
$numRows = $db->getNumRows();
$row = $db->loadObjectList();

// PRINT JSON FILE
header("Content-type: application/json");

for($i = 0 ; $i < $numRows; $i++){
$json_output[$i] = $row[$i];
}

$MYjson_output = json_encode($json_output);

echo $MYjson_output;

推荐答案

不完全是你的目标,但这里有 3-4 个可能有效的解决方案:

Not entirely such what your goal is but here are 3-4 solutions that might work:

这可能是为您的 API 获取干净 URI 的最传统方法.如果您希望 user.json 的用户部分是动态的,您可以使用 mod_rewrite(再次假设是 Apache)将您的 URL 重写为 php 处理程序脚本.如果您要使用传统的 RESTful 样式 URL 路由,您可能希望无论如何都使用它来实现干净的 / 分隔的 URL.

This is probaly the most conventional way of getting clean URIs for your API. If you want the user part of user.json to be dynamic, you can use mod_rewrite (again assuming Apache) to rewrite your URLs to a php handler script. If you are going the conventional RESTful style URL route, you will probably want to use this anyway to achieve clean / separated URLs.

# For grabbing all users
RewriteEngine on
RewriteRule ^users\.json rest.php [L]

# For grabbing one particular user
RewriteEngine on
RewriteRule ^([a-z0-9]+)\.json rest.php?user=$1 [L]

其中 rest.php 是您的 PHP 处理程序脚本.

Where rest.php is your PHP handler script.

如果你不想使用 mod_rewrite,你也可以做类似的事情

If you don't want to use mod_rewrite, you can also do something like

example.com/rest.php/users.json
example.com/rest.php/user-id.json

甚至

example.com/rest.php/user-id
example.com/rest.php/user/user-id

其中 rest.php 是您的 PHP 处理程序脚本.您可以使用 $_SERVER 全局和 $_SERVER['REQUEST_URI' 从 URL(或 URI,如果我们谈论 RESTful 术语)获取 user-id].

Where rest.php is your PHP handler script. You can grab the user-id from the URL (or URI if we're talking RESTful terms) using the $_SERVER global with $_SERVER['REQUEST_URI'].

我相信您想添加 Content-Disposition 标题...

I believe you want to add the Content-Disposition header...

<?php
    header('Content-Disposition: attachment; filename="users.json"');
?>

这将强制将文件下载为 user.json.这通常不是 REST API 预期的行为,但从您的问题的措辞来看,我想我会把它扔在那里.

This will force the file to be downloaded as user.json. This usually isn't the behavior expected for a REST API, but from the way your question was worded, I figured I'd throw it out there.

假设您正在运行 Apache 服务器,您也可以使用 AddHandler 指令使 .json 扩展文件被视为 php.

Assuming you're running an Apache server, you can also just use AddHandler directive to make .json extension files be treated like they are php.

AddHandler application/x-httpd-php .json

警告:其他纯 .json 文件将被视为 PHP,因此您可能希望将其设置在包含 PHP 脚本的目录中的 .htaccess 文件中.这对于这个 URI 来说可以正常工作,但如果您要公开许多 URI 则不理想

这篇关于PHP 中的 REST API 输出到 JSON 文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆