轻量级 RESTful PHP 服务器

发布于 2024-08-09 18:21:27 字数 780 浏览 3 评论 0原文

我想编写一个极其轻量级的 PHP 服务器来处理来自远程客户端的数据请求。返回的数据是表格形式的(类似于从 CSV 文件或数据库表中读取的数据)。 “问题”是我可能会返回数十万行数据 - 列宽在 10 - 15 之间(取决于请求的数据类型)。

简而言之,返回的数据可能巨大 - 为了节省带宽并提高传输速度,我想压缩数据(也许可以选择在我们使用时对其进行加密) ),然后将其发送回客户端。

我不知道如何编写服务器端脚本来处理请求(并将数据或错误代码发回)。

为了简单起见,我们假设我正在使用 fopen 从平面文件中读取数据,我可能会得到这样的结果:

<?php 
   // extract request variables and determine action required based on REQUEST params
   // handle request (fetch requested data)
   // if no error then return compressed (and encrypted?) data 
   // else if error return error code
?>

不太熟悉 PHP,有人可以帮我“充实”这个存根代码吗?多一点(特别是当我们通过 HTTP 标头等返回压缩数据(或错误代码)时的部分)?

最后但并非最不重要的一点是,我必须指出客户端可能在另一个平台和另一种语言上运行(我将用 C++ 编写客户端),因此我想使用纯 ASCI 文本进行数据传输(与 XML 相反,XML 非常冗长并且需要在另一端进行解析)。

I want to write an extremely lightweight PHP server which handles data requests from remote clients. The data returned is tabular (like that of data read from a CSV file or a database table). The "problem" is that I could be returning potentially several hundred thousand rows of data - with a column width of between 10 - 15 (depending on the type of data requested).

In short, the data returned could be HUGE - and in an attempt to save bandwidth, and also increase the speed of transmission, I would like to compress the data (maybe optionally encrypt it whilst we are at it) before sending it backto the client.

I am at a loss as to how to write the server side script to handle the request (and send the data or error code back).

For the sake of simplicity, lets assume that I am reading the data from a flat file, using fopen, I could have something like this:

<?php 
   // extract request variables and determine action required based on REQUEST params
   // handle request (fetch requested data)
   // if no error then return compressed (and encrypted?) data 
   // else if error return error code
?>

Not being very familiar with PHP, could someone please help me with "fleshing out" this stub code a little bit more (particularly the part when we are returning the compressed data (or error code) via HTTP headers etc)?.

Last but not the least, I have to point out that the client is likely to be running on another platform and another language (I will be writing the client in C++), so I would like to use PLAIN ASCI text for the data transfer (as opposed to XML which is very verbose and requires parsing at the other end).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唠甜嗑 2024-08-16 18:21:27

我可能建议您将 ZendFramework 用于 REST 服务器。基本约定已经为您实现,这将使您能够专注于您的特定需求(数据、文本、压缩等)。

以下是 Zend REST 的参考手册页:
http://framework.zend.com/manual/en/zend。 rest.server.html

另外,这是我根据我个人使用 Zend REST 的经验写的一篇博客文章:
http://ajcoon.blogspot.com/ 2009/09/rest-services-supporting-xml-and-json.html

即使我用它来返回 XML 和 JSON,人们也可以轻松定义自己的视图,为其数据使用不同的编码/格式。

I might suggest that you use the ZendFramework for your REST server. The basic conventions have already been implemented for you, and this will allow you to focus on your specific requirements (data, text, compression, etc).

Here is the reference manual page for Zend REST:
http://framework.zend.com/manual/en/zend.rest.server.html

Also, here's a blog post I wrote in my personal experience with using Zend REST:
http://ajcoon.blogspot.com/2009/09/rest-services-supporting-xml-and-json.html

Even though I used it to return XML and JSON, one could easily define their own view that uses a different encoding/format for their data.

魔法唧唧 2024-08-16 18:21:27

可以考虑以下可能性:将数据表上传到 Amazon SimpleDB,然后使用以下命令:http://blog.webservius.com/2010/09/14/introducing-amazon-simpledb-integration/(它会自动将任何 SimpleDB 表转换为功能齐全的 REST API,其中包含JSON 支持、开发人员注册页面等...)

Here's a possibility to consider: Upload the table of data to Amazon SimpleDB, and then use this: http://blog.webservius.com/2010/09/14/introducing-amazon-simpledb-integration/ (It automatically turns any SimpleDB table into a full-featured REST API complete with JSON support, developer signup page, etc, etc...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文