2017-08-08 11 views
0

私自身のAPIを実装しています。私はthe tutorial hereに従っています。私はそれに従っていますが、私はAPIを動作させるのに苦労しました。RestApi CodeIgniterを使用したポストリクエスト

CodeIgniter RESTサーバーとCodeIgniter RESTクライアントとの違いはありませんでした。誰かがそれを私に説明すれば大きな助けになるでしょう。

私の本当の問題は以下のとおりです。以下のコントローラがあり、チュートリアルで作成したREST_Controller.phpを拡張しています。

class Call extends REST_Controller 
{ 
    public function news() 
    { 
    // initialize you setting 
    $config = array(
     'server' => 'localhost' 
    ); 

    $this->rest->initialize($config); 

    // Set method of sending data 
    $method = 'post'; 


    // create your param data 
    $param = array(
     'id' => '1', 
     'name' => 'test' 
    ); 

    // url where you want to send your param data. 
    $uri = 'http://192.90.123.908/api_v1/index.php'; 

    // set format you sending data 
    $this->rest->format('application/json'); 

    // send your param data to given url using this 
    $result = $this->rest->{$method}($uri, $params); 

    $data=array(
     'id' => '1', 
     'name' => 'test' 
    ); 
    print_r($data); 
    } 
} 

私がこのURLにアクセスすると、予期したのはhttp://localhost/website/index.php/call/newsです。私はJSON応答を取得します。しかし、私が得るのは {"status":false,"error":"Unknown method"}です。私は何が間違っているのか分からない。

+0

をデバッグするためのツールPostman Development Environment。 – Tpojka

+0

REST_Controllerをそのまま使用するのではなく、独自のRESTクラスを実装する必要があると思います。要求の種類によって異なります。しかし、おそらく私たちはPOSTリクエストのみを送信しています。 – kishor10d

答えて

0

ダウンロードまたはドラッグhttps://github.com/chriskacerguis/codeigniter-restserver

ここから枝のクローンを作成し、アプリケーション/ライブラリ/ Format.phpおよびアプリケーション/ライブラリ/ REST_Controller.phpファイルにアプリケーションのディレクトリをドロップします。 require_onceをコントローラの上部で使用してスコープにロードします。さらに、アプリケーションの設定ディレクトリのapplication/configからrest.phpファイルをコピーします。

<?php 

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

class Call extends REST_Controller 
{ 
    public function news_get() 
    { 
    //Web service of type GET method 
    $this->response(["Hello World"], REST_Controller::HTTP_OK); 
    } 
    public function news_post() 
    { 
    //Web service of type POST method 
    $this->response(["Hello World"], REST_Controller::HTTP_OK); 
    } 
    public function news_put() 
    { 
    //Web service of type PUT method 
    $this->response(["Hello World"], REST_Controller::HTTP_OK); 
    } 
    public function news_delete() 
    { 
    //Web service of type DELETE method 
    $this->response(["Hello World"], REST_Controller::HTTP_OK); 
    } 
} 

使用あなたが7歳のチュートリアルを使用しているAPI

+0

私のすべてのリクエストがPOSTのみの場合はどうなりますか?リクエストごとに別々のコントローラを作成する必要がありますか?または_post()でメソッドを作成しますか? – kishor10d

+0

いいえあなたはメソッドを作成して_postをあなたのメソッドの名前に追加するだけで、同じコントローラを使用できます –

関連する問題