我在调用用Slim编写的API时犯了什么错误?

时间:2015-01-20 10:13:01

标签: php web-services rest request slim

我正在使用Slim框架从现有的PHPFox网站访问这些功能。我只是Slim框架的新手,所以很想学习它。

我已经包含了必要的文件。但我面临的问题是,在提出请求时,我总是会收到“404 Page Not Found”错误。即使我尝试在创建对象后打印“对象创建”消息,它也会被打印,这意味着直到此时没有错误。当我尝试通过回显一些消息进入函数时,我得到404错误。

有人可以帮我纠正我在代码中犯的错误吗?

以下是我的代码( login_ws.php ):

<?php

    require 'Slim/Slim.php';
    require('config.php');

    \Slim\Slim::registerAutoloader();

    //Instantiate Slim class in order to get a reference for the object.
    $application = new \Slim\Slim();
    //Object created
    $application->map (
        '/login/:login/:password/:divice_type', 

        function()
        { echo "Ina funcionality"; die;
            global $application;
            if($application->request->isGet()) {
                $login        = $application->request->get('login');
              $password     = $application->request->get('password');
              $divice_type  = $application->request->get('divice_type');
              /*$divice_token = $application->request->get('divice_token');*/
            }  
            if($application->request->isPost()) {
                $login        = $application->request->post('login');
              $password     = $application->request->post('password');
              $divice_type  = $application->request->post('divice_type');
              /*$divice_token = $application->request->post('divice_token');*/
            }
            list($bLogged, $aUser) = Phpfox::getService('user.auth')->login($login, $password);
            if($aUser['user_image']) {
            $aUser['user_image'] = Phpfox::getParam('core.url_user').$aUser['user_image'];
        } else {
                $aUser['user_image'] = '';
            }


            if((!empty($aUser)) && ($bLogged>0)) {
                $msg='Login succesfully';
                Phpfox::getService('user.auth')->setUserId($aUser['user_id']);
                $appKey = Phpfox::getService('apps')->getKey('3');

                $test = doPost("http://localhost/token.php?key=".$appKey);
                $arrayToken = json_decode($test);

              $aUser['token'] = $arrayToken->token;
                $aUser['key'] = $appKey;
              } else {
                $msg='Login Failed';
                $aUser[]=$login;
                $aUser[]=$password;
            }
              $result=array('status'=>$bLogged ? 'true' : 'false','data'=>$aUser,'msg'=>$msg);
              header("Content-Type: application/json");
              echo json_encode($result);
        }
    )->via('GET','POST');
    //Purpose of via() : The function will be invoked when there is a match for the URI whether the request is using HTTP GET or HTTP POST method.      
    $application->run();

    function doPost($aParams) {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_URL, $aParams);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);

        $result = curl_exec($ch);
        curl_close($ch);        
        return $result;
    }
?>

我通过浏览器的URL发出的请求如下:

http://localhost/webservice/login_ws.php/login?login=admin@gmail.com&password=admin123&divice_type=iphone

请帮助我纠正我正在犯的错误。此外,如果你发现我正在做任何错误的方式,这不是Slim框架的标准,请纠正我。我会为此感到高兴。

感谢。

0 个答案:

没有答案