Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
REST概念的澄清(使用PHP)_Php_Rest_Curl - Fatal编程技术网

REST概念的澄清(使用PHP)

REST概念的澄清(使用PHP),php,rest,curl,Php,Rest,Curl,我对REST的一些概念有点困惑,我希望能澄清一下。我遵循这个教程,所以我使用的任何代码都来自它 1)。每当我想发布数据时,我是否必须完成curl事务的整个过程? <?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( a

我对REST的一些概念有点困惑,我希望能澄清一下。我遵循这个教程,所以我使用的任何代码都来自它

1)。每当我想发布数据时,我是否必须完成curl事务的整个过程?

<?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);
<?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");
}
按顺序:

  • 您可以编写一个函数来封装整个卷曲小曲

  • 不确定
    $resource
    $id
    指向的是什么,但这样做似乎有意义;请注意,URL应该以http://
  • 或https://开头

  • 该代码看起来像一个典型的路由器,应该在每次请求时被调用;确保在编写代码之前设置自动加载程序,以便自动加载类

  • 所有资源类都将从该基类扩展;通过在类中创建
    公共函数get(){}
    ,路由器将在
    get
    方法的情况下自动调用该方法;无论您未实现什么,都将导致运行
    \u call()
    ,这将向REST客户端提供错误代码


  • 响应Q4—我最喜欢的方法是创建一个表示HTTP进程的接口,并在对调用的资源执行该方法之前,使用
    method\u 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);
    

    如果你问一个关于问题的特定问题,堆栈溢出效果最好。那么我应该在哪里发布呢?我问了一些关于这个概念的具体问题。听起来你对这个概念理解得很好,你只是有几个关于如何构造代码来实现这个概念的问题。你能把它归结为一个常见的问题还是分成多个问题?@deceze我想我最困惑的是上面的问题#4。正如许多事情一样,多方向优化会导致客观矛盾。你想达到什么目标?PHP中的REST API“性能最佳”/“易于使用”/“易于扩展”/“非常基本”这种方法的缺陷在于,应用程序中的每个资源都不支持相同的方法,除非是在非常基本的cookie-cuter实现中。此外,HTTP是可扩展的。i、 e.可以使用自定义方法,如
    ZANZIBAR
    。这就是为什么我们可以有WEBDAV之类的东西。为什么
    Resource
    类及其子类不支持相同的方法?您可以将
    Resource
    声明为实现
    HTTP
    。在灵活性方面,静态方法确实更有用。您还可以创建一个基本接口(例如
    HTTP
    )来实现常用方法,并使用基于HTTP协议的各种其他接口扩展该接口。事实上,这将促使我们需要找出每个
    资源
    实现了哪些接口,并对其进行测试,这使得过程更加复杂。简单的例子:我的
    /
    资源只支持
    获取
    。它不接受
    POST
    PUT
    DELETE
    。它所做的只是提供超文本信息,告知客户可以从服务中获得哪些资源。我是否应该有一个只支持
    GET
    的界面,然后扩展该界面以支持
    POST
    以获得支持
    GET
    POST
    的资源?使用单一接口——尽管这是您通常从框架“REST”实现中看到的——只对cookie cutter解决方案有用?(然后将包含在每页上)#3自动加载器只是一个类构造函数,对吗?或者它是类构造函数的构造函数?是的,可能是。对于自动加载器,请查看
    spl\u autoload\u register()
    @user1104854 np~玩得开心:)还有一个问题-在上面的第1个问题上,$uri在顶部声明,但是当curl事务发生时,会使用一个名为$url的变量。这是一个错误(请记住,此代码是从教程中复制的)还是我没有看到什么?对我来说,这似乎是一个打字错误:)
    <?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(), ", "));
    }
    }
    
    // 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);