休息网络服务? [英] Rest Web Services?

查看:41
本文介绍了休息网络服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算创建一个宁静的网络服务 (PHP).

I am planning to create a restful web service (PHP).

api/user
api/user/[id]
api/ad
api/ad/[id];
etc

用户"和广告"是实际的 php 文件吗?如果不是,我怎么知道(在我的 php 文件中)调用者调用的是用户"还是广告"?关于如何创建一个安静的 Web 服务的任何好的教程?大多数教程只涵盖了 REST Web 服务的基础知识.

are "user" and "ad" actual php files? and if not how can i tell (on my php file) which one the caller is calling "user" or "ad"? any good tutorial on how to create a restful web service? Most tutorials only cover the basics of rest web services.

推荐答案

好吧,对于简单的服务,您有一个脚本文件(例如名为 index.php)作为分派函数的各种路径(当然可能在其他 PHP 文件中).使用您提到的路径之一附加在脚本名称之后调用它.在脚本中,您可以使用 $_SERVER['PATH_INFO'] 来确定该路径.

Well, for simple services, you have a single script file (e.g. named index.php) as the entry point that dispatches the various paths to your functions (which may of course be in other PHP files). It is called with one of the paths you mentioned appended after the script's name. In the script, you can use $_SERVER['PATH_INFO'] to determine the that path.

您可以通过制作一个仅包含此内容的脚本来对此进行一些试验:

You could experiment with this a little by making a script with only this content in it:

<?php
    phpinfo();
?>

如果您在浏览器中从您的 Web 服务器请求此脚本,您将看到所有相关数据打印在一个漂亮的列表中.向下滚动到 PHP 变量 部分以查看您的 PATH_INFO.

If you request this script in a browser from your web server, you will see all relevant data printed in a nice list. Scroll down to the PHP Variables section to see your PATH_INFO.

请参阅http://php.net/manual/en/reserved.variables.server.php


此外,将访问方法限制为 HTTP GETPOST 让您的生活更轻松,因为 PHP 具有 $_GET$_POST变量但不是例如$_PUT/$_DELETE 等.但是,您可以发送带有指定方法的特殊变量的 POST 请求,以便模拟 PUTDELETEHEADOPTIONS 操作(例如 _method=PUT).


Additionally, restricting access methods to HTTP GET and POST makes your life easier because PHP has $_GET and $_POST variables but not e.g. $_PUT / $_DELETE and the like. However, you could send POST requests with a special variable specifying the method so to emulate PUT, DELETE, HEAD or OPTIONS operations (e.g. _method=PUT).

当支持除GETPOST之外的其他方法时,使用$_SERVER['REQUEST_METHOD']来确定使用的方法并读取数据与来自特殊文件"php://input 的请求一起发送(例如使用 file_get_contents('php://input')).

When supporting other methods than GET and POST, use $_SERVER['REQUEST_METHOD'] to determine the method used and read the data sent along with the request from the special "file" php://input (e.g. with file_get_contents('php://input')).


正如 tdammers 所指出的,Apache RewriteRules 有助于使您的 URL 变得漂亮,在这种情况下可以像这样隐藏脚本的名称(取决于当然,在您脚本的位置):


As tdammers noted, Apache RewriteRules are useful to make your URLs pretty, in this case to hide the script's name like this (depends on your script's location, of course):

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/api/index\.php(/|$)
RewriteRule ^/api/(.*)$ /api/index.php/$1 [QSA,L]

这篇关于休息网络服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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