【问题标题】:How restserver and restclient working together in CodeIgniter在 CodeIgniter 中 restserver 和 restclient 如何协同工作
【发布时间】:2015-08-17 23:34:13
【问题描述】:

如何设置我阅读了教程http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814。但我无法理解,我想要更多细节。我对 CodeIgniter 和 API 非常陌生。

我从 nettuts 文章中做了以下步骤

  • 下载 restclient 和 restserver 和 curl
  • 我尝试从 rest-server 运行示例,但它没有向我显示任何内容。我加载自己的控制器和方法

【问题讨论】:

  • 您是否尝试过使用默认教程代码?有用吗?
  • 感谢回复。是的,我首先尝试示例
  • 它不适用于全新的 CI 安装和默认教程文件/代码?你这么说吗?
  • 我只从 rest-server 运行示例,它给了我错误使用 ci 版本 3 而我使用的是 ci 版本 2.2
  • 你会尝试什么?您需要编辑您的问题并写下您正在使用的代码,以帮助我们了解问题出在哪里。我喜欢提到this source,因为它帮助了我和很多人。

标签: php codeigniter rest-client codeigniter-restserver


【解决方案1】:

休息服务器:

这是监听客户端(restClient)请求的服务器。 RESTServer 具有请求方法: POST()
获取()
放()
删除()

这些用法类似于index_put(); 请记住,当您从 RESTClient 调用它时,您会像这样调用它:

$this->index();

不是

$this->index_put(); //because restserver it self recognize the nature of request through header.

这是一个简单的例子:

REST客户端:

function request_test() {
        $this->load->library('rest', array(
            'server' => 'http://restserver.com/customapi/api/',
             //when not use keys delete these two liness below
            'api_key' => 'b35f83d49cf0585c6a104476b9dc3694eee1ec4e',
            'api_name' => 'X-API-KEY',
        ));
        $created_key = $this->rest->post('clientRequest', array(
            'id' => '1',
            'CustomerId' => '1',
            'amount' => '2450',
            'operatorName' => 'Jondoe',
        ), 'json');
        print_r($created_key);
        die;

    }
  • 确保您加载了 rest 库。

休息服务器:

<?php
require APPPATH . '/libraries/REST_Controller.php';

class api extends REST_Controller {
  public function clientRequest_post() {
    //to get header 
    $headers=array();
    foreach (getallheaders() as $name => $value) {
        $headers[$name] = $value;
    }
    //to get post data
    $entityBody = file_get_contents('php://input', 'r');
    parse_str($entityBody , $post_data);

    //giving response back to client 
    $this->response('success', 200);


  }
}

配置config/Rest.php:

 //if you need no authentication see it's different option in the same file
    $config['rest_auth'] = false;

 //for enabling/disabling API_KEYS
$config['rest_enable_keys'] = FALSE;

【讨论】:

    猜你喜欢
    • 2014-09-01
    • 2011-10-28
    • 1970-01-01
    • 2016-08-18
    • 2018-01-16
    • 1970-01-01
    • 2021-12-09
    • 2019-04-21
    • 2014-08-05
    相关资源
    最近更新 更多