如何使用CURL调用REST

时间:2014-07-24 14:13:09

标签: php .htaccess rest curl

我第一次使用休息api卷曲。我创建了一个api.php,我使用curl和post方法调用此文件。在第68行我得到一个错误rquest变量发出通知而没有得到他的价值'登录'用方法发布。这是我的想法。

在页面上的curl请求后,api.php rquest var值将被登录,我们将在rquest值的帮助下调用login函数。

Htaccess是:

<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>

我有一个带REST类的父文件rest.php

api.php

class API extends REST {
        public $data = "";
        const DB_SERVER = "localhost";
        const DB_USER = "root";
        const DB_PASSWORD = "";
        const DB = "test";

        private $db = NULL;

        public function __construct(){
            parent::__construct();              // Init parent contructor
            $this->dbConnect();                 // Initiate Database connection
        }

        /*
         *  Database connection 
        */
        private function dbConnect(){
            $this->db = mysql_connect(self::DB_SERVER,self::DB_USER,self::DB_PASSWORD);
            if($this->db)
                mysql_select_db(self::DB,$this->db);
        }

        /*
         * Public method for access api.
         * This method dynmically call the method based on the query string
         *
         */
        public function processApi(){

            $func = strtolower(trim(str_replace("/","",$_REQUEST['rquest']))); // **line no: 68**
            if((int)method_exists($this,$func) > 0)
                $this->$func();
            else
                $this->response('',404);                // If the method not exist with in this class, response would be "Page not found".
        }

        /* 
         *  Simple login API
         *  Login must be POST method
         *  email : <USER EMAIL>
         *  pwd : <USER PASSWORD>
         */

            private function login(){
        // Cross validation if the request method is POST else it will return "Not Acceptable" status

        if($this->get_request_method() != "POST"){
             $this->response('',406);
        }

        $email = $this->_request['email'];
        $password = $this->_request['pwd'];

        // Input validations
        if(!empty($email) and !empty($password)){
            if(filter_var($email, FILTER_VALIDATE_EMAIL)){
                $sql = mysql_query("SELECT user_id, user_fullname, user_email FROM users WHERE user_email = '$email' AND user_password = '".$password."' LIMIT 1", $this->db);
                if(mysql_num_rows($sql) > 0){
                    $result = mysql_fetch_array($sql,MYSQL_ASSOC);

                    // If success everythig is good send header as "OK" and user details
                    $this->response($this->json($result), 200);
                }
                $this->response('', 204);   // If no records "No Content" status
            }
        }

        // If invalid inputs "Bad Request" status message and reason
        $error = array('status' => "Failed", "msg" => "Invalid Email address or Password");
        $this->response($this->json($error), 400);
    }

    }

    // Initiiate Library

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

我使用curl调用api.php是

的index.php

<?php
$url = 'http://localhost/test/login/';
        $curl = curl_init();
        $curl_post_data = array(
            "pwd" => 'raj123',
            "emailaddress" => 'rajk@gmail.com',
            );
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); // 
        curl_setopt($curl, CURLOPT_POST, true)
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
        $curl_response = curl_exec($curl);
        curl_close($curl);
        echo $curl_response;
        //$xml = new SimpleXMLElement($curl_response);

    // code adapted from Tony Spencer

?>

错误即将来临

<b>Notice</b>:  Undefined index: rquest in <b>C:\xampp\htdocs\test\api.php</b> on line <b>68</b><br />

api.php中的第68行

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

源链接为http://www.9lessons.info/2012/05/create-restful-services-api-in-php.html

0 个答案:

没有答案