我对REST的一些概念感到困惑,我很欣赏一些澄清。我正在学习本教程,因此我使用的任何代码都来自它。 http://phpmaster.com/rest-can-you-do-more-than-spell-it-3/
1)。无论何时我想发布数据,我是否必须完成卷曲交易的整个过程?
<?php
// set up the URI value involved in a variable
$uri = "http://www.funland.com/summerschedule";
// set up the data that is going to be passed
$events = array(
array("event" => "20120601-0001",
"name" => "AC/DC Drink Till U Drop Concert",
"date" => "20120601",
"time" => "22000030"),
array("event" => "20120602-0001",
"name" => "Enya – Can You Feel the Peace",
"date" => "20120602",
"time" => "19300045"),
array("event" => "20120603-0002",
"name" => "Nicki Menaj – The Midnight Girls Concerrtt",
"date" => "20120603",
"time" => "21300039"));
$jsonData = json_encode($events)
// perform the curl transaction
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($ch);
$decode = json_decode($response);
print_r($resonse);
2。每当我创建一个链接时,就URI而言,我是否需要做一些特别的事情,或者我只是根据它的访问方式对其进行格式化?
要建立指向www.example.com/restaurant/42的链接,我会建立以下链接,还是我还有其他的事情要做?
//assuming $resource = restaurant in this example
<a href ="www.example.com/".$resource."/".$id">Item 42</a>
第3。对于解析URL路径的代码,我是否在每个文件中都有这个,或者我是否会创建一个文件(例如api.php)并将其包含在每个文件中?
<?php
// assume autoloader available and configured
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$path = trim($path, "/");
@list($resource, $params) = explode("/", $path, 2);
$resource = ucfirst(strtolower($resource));
$method = strtolower($_SERVER["REQUEST_METHOD"]);
$params = !empty($params) ? explode("/", $params) : array();
if (class_exists($resource)) {
try {
$resource = new $resource($params);
$resource->{$method}();
}
catch (Exception $e) {
header("HTTP/1.1 500 Internal Server Error");
}
}
else {
header("HTTP/1.1 404 File Not Found");
}
4。我在哪里为某个类的每个函数设置可接受的“动词”?以下代码来自上面链接的教程的第2部分。我看到有一个类构造函数,但是我应该为每个函数指定可接受的操作吗?也许我误解了这段代码,但我不知道它在哪里说DELETE不被接受像Restaurant / id这样的东西
<?php
abstract class Resource
{
protected static $httpMethods = array("GET", "POST", "HEAD",
"PUT", "OPTIONS", "DELETE", "TRACE", "CONNECT");
protected $params;
public function __construct(array $params) {
$this->params = $params;
}
protected function allowedHttpMethods() {
$myMethods = array();
$r = new \ReflectionClass($this);
foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $rm) {
$myMethods[] = strtoupper($rm->name);
}
return array_intersect(self::$httpMethods, $myMethods);
}
public function __call($method, $arguments) {
header("HTTP/1.1 405 Method Not Allowed", true, 405);
header("Allow: " . join($this->allowedHttpMethods(), ", "));
}
}
答案 0 :(得分:2)
按顺序:
您可以编写一个封装整个cURL小曲的函数。
不确定$resource
和$id
指向的是什么,但似乎有道理;请注意,网址应以http://
或https://
开头。
该代码看起来像一个典型的路由器,应该在每个请求时被调用;确保在该代码之前设置自动加载器,以便自动加载类。
所有资源类都将从该基类扩展;通过在您的班级中创建public function get() { }
,路由器将在GET
方法的情况下自动调用该方法;你未实现的任何内容都会导致__call()
运行,这将为REST客户端提供错误代码。
答案 1 :(得分:0)
响应Q4 - 我最喜欢的方法是创建一个表示HTTP进程的接口,并在对被调用资源执行方法之前使用method_exists()
检查该接口。
// declaring the methods
interface HTTP {
public function get();
public function post();
public function put();
....
}
// checking if the attempted method is allowable
method_exists('HTTP', $method);