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

查看:91
本文介绍了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(如果使用的是RESTful术语,则为URI)中获取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天全站免登陆