2017-09-26 7 views
0

チュートリアルhereに従って、PHPでRESTful APIを作成しました。

Notice: Undefined index: request in C:\wamp64\www\API\api.php on line 8 

(注:私は、しかし私は、エンドポイントを表示しようとすると、次のエラーを取得していますローカルホスト上のプロジェクトを実行している

は、チュートリアル(API/V1 /例)で提案されている参照しますapi.phpファイルをダウンロードしてください)

このリクエストインデックスは、htaccessファイルに設定する必要があります。問題がどこにあるのかと思います。 (これは単純なことだと私の謝罪がありますが、運がなければ自分自身を見つけようとしています)。

  1. Webサーバは、プロジェクトのディレクトリ内のファイルへのリクエストを処理する方法を設定するには、htaccessのファイル:私たちが作成チュートリアルで

  2. URIの解析を処理し、 応答を返すための抽象クラスです。
  3. APIのエンドポイントで構成されるコンクリートクラス。

のhtaccessファイル

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule api/v1/(.*)$ api/v1/api.php?request=$1 [QSA,NC,L] 
</IfModule> 

抽象クラス

<?php 
    abstract class API 
    { 
     /** 
     * Property: method 
     * The HTTP method this request was made in, either GET, POST, PUT or DELETE 
     */ 
     protected $method = ''; 
     /** 
     * Property: endpoint 
     * The Model requested in the URI. eg: /files 
     */ 
     protected $endpoint = ''; 
     /** 
     * Property: verb 
     * An optional additional descriptor about the endpoint, used for things that can 
     * not be handled by the basic methods. eg: /files/process 
     */ 
     protected $verb = ''; 
     /** 
     * Property: args 
     * Any additional URI components after the endpoint and verb have been removed, in our 
     * case, an integer ID for the resource. eg: /<endpoint>/<verb>/<arg0>/<arg1> 
     * or /<endpoint>/<arg0> 
     */ 
     protected $args = Array(); 
     /** 
     * Property: file 
     * Stores the input of the PUT request 
     */ 
     protected $file = Null; 

     /** 
     * Constructor: __construct 
     * Allow for CORS, assemble and pre-process the data 
     */ 
     public function __construct($request) { 
      header("Access-Control-Allow-Orgin: *"); 
      header("Access-Control-Allow-Methods: *"); 
      header("Content-Type: application/json"); 

      $this->args = explode('/', rtrim($request, '/')); 
      $this->endpoint = array_shift($this->args); 
      if (array_key_exists(0, $this->args) && !is_numeric($this->args[0])) { 
       $this->verb = array_shift($this->args); 
      } 

      $this->method = $_SERVER['REQUEST_METHOD']; 
      if ($this->method == 'POST' && array_key_exists('HTTP_X_HTTP_METHOD', $_SERVER)) { 
       if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'DELETE') { 
        $this->method = 'DELETE'; 
       } else if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'PUT') { 
        $this->method = 'PUT'; 
       } else { 
        throw new Exception("Unexpected Header"); 
       } 
      } 

      switch($this->method) { 
      case 'DELETE': 
      case 'POST': 
       $this->request = $this->_cleanInputs($_POST); 
       break; 
      case 'GET': 
       $this->request = $this->_cleanInputs($_GET); 
       break; 
      case 'PUT': 
       $this->request = $this->_cleanInputs($_GET); 
       $this->file = file_get_contents("php://input"); 
       break; 
      default: 
       $this->_response('Invalid Method', 405); 
       break; 
      } 
     } 
     public function processAPI() { 
      if (method_exists($this, $this->endpoint)) { 
       return $this->_response($this->{$this->endpoint}($this->args)); 
      } 
      return $this->_response("No Endpoint: $this->endpoint", 404); 
     } 

     private function _response($data, $status = 200) { 
      header("HTTP/1.1 " . $status . " " . $this->_requestStatus($status)); 
      return json_encode($data); 
     } 

     private function _cleanInputs($data) { 
      $clean_input = Array(); 
      if (is_array($data)) { 
       foreach ($data as $k => $v) { 
        $clean_input[$k] = $this->_cleanInputs($v); 
       } 
      } else { 
       $clean_input = trim(strip_tags($data)); 
      } 
      return $clean_input; 
     } 

     private function _requestStatus($code) { 
      $status = array( 
       200 => 'OK', 
       404 => 'Not Found', 
       405 => 'Method Not Allowed', 
       500 => 'Internal Server Error', 
      ); 
      return ($status[$code])?$status[$code]:$status[500]; 
     } 



    } 
    ?> 

コンクリートクラス

<?php 
require_once 'API.class.php'; 

class MyAPI extends API 
{ 
    protected $FacebookUser; 

    public function __construct($request, $origin) { 
     parent::__construct($request); 
     //I removed the User and API key code here until they are set up properly 
    } 

    /** 
    * Example of an Endpoint 
    */ 
    protected function example() { 
     if ($this->method == 'GET') { 
      return "Endpoint is working"; 
     } else { 
      return "Only accepts GET requests"; 
     } 
    } 
} 
?> 

api.php

<?php 
require_once 'MyAPI.class.php'; 
// Requests from the same server don't have a HTTP_ORIGIN header 
if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) { 
    $_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME']; 
} 
try { 
    //the following $_REQUEST['request'] variable is not set. 
    $API = new MyAPI($_REQUEST['request'], $_SERVER['HTTP_ORIGIN']); 
    echo $API->processAPI(); 
} catch (Exception $e) { 
    echo json_encode(Array('error' => $e->getMessage())); 
} 
?> 
+0

送信リクエスト時に変数を送信していますか? –

+0

'api/v1/foo'にアクセスしても動作しますか? – Progrock

+0

@Progrock私はそれを試しました。同じエラーが発生します。 – Sarah

答えて

1

あなたはより多くのこのような階層構造が必要になりますあなたの.htaccessファイルを読む:

APIは(将来的にあなたはおそらくAPIドキュメントルートや移動を行います現在、ドキュメントルートのサブフォルダである
API 
├── api 
│   └── v1 
│    └── api.php 
└── .htaccess 

それに応じたファイル)。 API/.htaccess

内容:API/api/v1/api.php

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule api/v1/(.*)$ api/v1/api.php?request=$1 [QSA,NC,L] 

目次(リライトをチェックするために取り組んでいる):

<?php 
var_dump($_REQUEST); 

は、その後の要求:

example.com/API/api/v1/foo 

は、あなたのAPIに書き換えるべき。phpファイル(書き換えルールを介して)と出力する必要があります:

array(1) { ["request"]=> string(3) "foo" } 
+0

ありがとうございます。あなたの答えからインスピレーションを得て、私はファイル構造を変更しませんでした。次のように私は.htaccessファイルを変更:? <のIfModule mod_rewrite.c> のRewriteRuleでRewriteEngine(。*)$ api.php要求= $ 1 [QSA、NC、L] 、これは今働いています(リクエスト変数は、http:// localhost/API/exampleまたはhttp:// localhost/API/api/v1/exampleにアクセスするときに設定されています。ご覧のとおり、2つのRewriteCondラインは、RewriteRuleが進まないため削除しました。あなたのすべての協力に感謝します。 – Sarah

+1

はい、そういうことができます。私は他の2つのラインを保つだろう、それは私のためにうまく動作します。 – Progrock

+0

ありがとうございます:) – Sarah

1

$_REQUESTは、クライアントによって送信された要求変数の配列です。キーrequestは、この配列の内部に存在するとは限りません。デバッガまたはvar_dumpを使用して、配列の内容を表示しようとしています。

おそらく、送信しているリクエストで質問を更新すると、誰かがさらにあなたを導くことができます。

+0

ありがとうございます。プロセスの私の理解は、リクエスト変数がhtaccessファイルにここで設定されているということです:(RewriteRule api/v1 /(.*)$api/v1/api.php?request = $ 1 [QSA、NC、L]) $ this-> args = explode( '/'、rtrim($ request、 '/'))この抽象API.classのコンストラクタで解析される完全なURLを含みます。 ); ...そこからURI文字列からエンドポイントを取得することです。私は$ _REQUEST変数を出力しましたが、それは空です...あなたはそれが正しく書き換えを実行していない私のhtaccessだと思いますか? – Sarah

関連する問題