doujianqin5172 2017-06-03 00:22
浏览 39
已采纳

使用Amazon EC2,我仍然可以使用Rest.inc.php(REST API),还是有不同的方法?

I'm trying to migrate my REST APIs for a project to Amazon AWS. I have PHP 7.0 installed and working on an EC2 instance. However, I cannot seem to get my API routing working at all. The original version uses Rest.inc.php and .htaccess file. I've set this up on several other servers without any problem, but can't get it working under Amazon AWS.

What I'm wondering is where the problem lies. I'm just getting a not found error on the API calls. Is this likely related to security/signing issues, or should I be using a completely different approach on AWS?

The code I'm using is very similar to the example that seems to popup everywhere you search.

<?php

    require_once("Rest.inc.php");

    class API extends REST {

        public $data = "";

        const DB_USER       = "username";
        const DB_PASSWORD   = "password";

        private $db = NULL;

        public function __construct(){
            parent::__construct();
            $this->dbConnect();
        }

        private function dbConnect(){
            try {
                $this->db = new PDO("mysql:host=myAWShost;dbname=database", self::DB_USER, self::DB_PASSWORD);
                $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            catch(PDOException $e) {
                echo "Connection failed: " . $e->getMessage();
            }
        }

        public function processApi(){

            $func = strtolower(trim(str_replace("/","",$_REQUEST['rquest'])));

            if((int)method_exists($this,$func) > 0) {
                $this->$func();
            } else {
                $this->response('',404);
            }

        }

        private function activity(){
            ...
        }

        private function appointment(){
            ...
        }

        private function client(){
            ...
        }

        private function facility(){
            ...
        }

        private function person(){  
            ...
        }

        private function personLocation(){
            ...         
        }
    }

    // Initiiate Library

    $api = new API;
    $api->processApi();

?>

.htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteRule ^(.*)$ api.php?rquest=$1 [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*)$ api.php [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} -s
    RewriteRule ^(.*)$ api.php [QSA,NC,L]   
</IfModule>
  • 写回答

1条回答 默认 最新

  • dongxiqian2787 2017-06-03 01:17
    关注

    For Apache to read the .htaccess file you need to ensure that AllowOverride All is set within your Apache config file, and then restart apache with sudo service httpd restart.

    Example:

    <Directory "/var/www/html">
    AllowOverride None
    </Directory>
    

    Becomes:

    <Directory "/var/www/html">
    AllowOverride All
    </Directory>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码