PHP中的REST API输出到JSON文件扩展名

时间:2022-10-17 11:10:24

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:

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

1mdi2o3.part

How do I return something like: users.json

我如何返回类似于: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 个解决方案

#1


10  

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

并非完全符合您的目标,但这里有3-4个可能有效的解决方案:

Common Solutions

Rewrite

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.

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

# 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]

Where rest.php is your PHP handler script.

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

URL Rewriting without mod-rewrite

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

如果你不想使用mod_rewrite,你也可以这样做

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

or even

example.com/rest.php/user-id
example.com/rest.php/user/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'].

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

Other solutions

Changing download name via Content-Disposition:

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

我相信你想添加Content-Disposition标头......

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

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.

这将强制将文件作为user.json下载。这通常不是REST API的预期行为,但从你的问题措辞的方式来看,我想我会把它扔出去。

AddHandler (or AddType)

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.

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

AddHandler application/x-httpd-php .json

Warning: Other plain .json files will be treated as PHP so you'd probably want to set this in a .htaccess file in the directory with the PHP script. This would work fine for this one URI but not ideal if you were exposing many URIs

警告:其他普通.json文件将被视为PHP,因此您可能希望使用PHP脚本在目录中的.htaccess文件中设置它。这对于这个URI可以正常工作,但如果你暴露了许多URI则不理想

#2


-2  

Welcome to SO. Have you considered using the Zend framework for this application? I've written about this topic before, and if you do use Zend I could probably be of additional help. It would certainly help get you past the basics.

欢迎来到SO。您是否考虑过将Zend框架用于此应用程序?我以前写过关于这个主题的文章,如果你使用Zend,我可能会有额外的帮助。它肯定会帮助你超越基础。

HTH,

-aj

#3


-2  

The problem is this line:

问题是这一行:

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

I know this looks like the right way to do (after all, application/json is the official MIME type for JSON content), but it causes most browsers to present you with a file download dialog, when you just want to see the text. You can use the text/plain encoding to avoid this. Note that your AJAX/iPhone/... app probably doesn't care about the content type, so your API will work in both cases.

我知道这看起来是正确的方法(毕竟,application / json是JSON内容的官方MIME类型),但是当你只想查看文本时,它会导致大多数浏览器向你展示文件下载对话框。您可以使用text / plain编码来避免这种情况。请注意,您的AJAX / iPhone / ...应用程序可能并不关心内容类型,因此您的API将在两种情况下都有效。

Also see this blog post which provides some more context.

另请参阅此博客文章,其中提供了更多上下文。

#1


10  

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

并非完全符合您的目标,但这里有3-4个可能有效的解决方案:

Common Solutions

Rewrite

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.

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

# 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]

Where rest.php is your PHP handler script.

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

URL Rewriting without mod-rewrite

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

如果你不想使用mod_rewrite,你也可以这样做

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

or even

example.com/rest.php/user-id
example.com/rest.php/user/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'].

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

Other solutions

Changing download name via Content-Disposition:

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

我相信你想添加Content-Disposition标头......

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

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.

这将强制将文件作为user.json下载。这通常不是REST API的预期行为,但从你的问题措辞的方式来看,我想我会把它扔出去。

AddHandler (or AddType)

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.

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

AddHandler application/x-httpd-php .json

Warning: Other plain .json files will be treated as PHP so you'd probably want to set this in a .htaccess file in the directory with the PHP script. This would work fine for this one URI but not ideal if you were exposing many URIs

警告:其他普通.json文件将被视为PHP,因此您可能希望使用PHP脚本在目录中的.htaccess文件中设置它。这对于这个URI可以正常工作,但如果你暴露了许多URI则不理想

#2


-2  

Welcome to SO. Have you considered using the Zend framework for this application? I've written about this topic before, and if you do use Zend I could probably be of additional help. It would certainly help get you past the basics.

欢迎来到SO。您是否考虑过将Zend框架用于此应用程序?我以前写过关于这个主题的文章,如果你使用Zend,我可能会有额外的帮助。它肯定会帮助你超越基础。

HTH,

-aj

#3


-2  

The problem is this line:

问题是这一行:

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

I know this looks like the right way to do (after all, application/json is the official MIME type for JSON content), but it causes most browsers to present you with a file download dialog, when you just want to see the text. You can use the text/plain encoding to avoid this. Note that your AJAX/iPhone/... app probably doesn't care about the content type, so your API will work in both cases.

我知道这看起来是正确的方法(毕竟,application / json是JSON内容的官方MIME类型),但是当你只想查看文本时,它会导致大多数浏览器向你展示文件下载对话框。您可以使用text / plain编码来避免这种情况。请注意,您的AJAX / iPhone / ...应用程序可能并不关心内容类型,因此您的API将在两种情况下都有效。

Also see this blog post which provides some more context.

另请参阅此博客文章,其中提供了更多上下文。