如何使用CodeIgniter在RESTful API中发送post值?

时间:2016-05-12 06:19:29

标签: php codeigniter api

我不知道如何获取值,请帮我解决这个问题。如果某人已经拥有该代码,请告诉我,请分享。我也很好奇如何使用带有完整过程的RESTful API来加载cURL的火花

<?php class LoginController extends CI_Controller {

    public function index()
    {
        $this->load->view('admin/header');
        $this->load->view('admin/index');
        $this->load->view('admin/footer');
    }

    public function loginCon(){

$this->load->Library('rest');

        $this->form_validation->set_rules('email', 'E-mail', 'required|trim');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_error_delimiters("<p class='text-danger'>", "</p>");

        if ($this->form_validation->run()==false)
        {
            $email = $this->input->post('email');
            $password = $this->input->post('password');
            $this->session->set_flashdata('login_failed', 'Invalid User Name Password');

        }else{

            $config = array('server' => "http://api.amid.tech/hsApiV2/api/demo.php/",

                'http_user' => 'admin',
                'http_pass' => 'xxxxx',
                'http_auth' => 'basic',
            );
            $this->rest->initialize($config);
            $method = 'post';
            $param = array(
                'UserEmail' => $this->input->post('email'), // works fine here
                'UserPass' => $this->input->post('password'),
                'UserRoleId'=>1
            );
            $uri = 'adminlogin';
            $this->rest->format('application/json');
            $result = $this->rest->{$method}($uri, $param);
            echo $result;
          $this->load->view('admin/admin_header');
            $this->load->view('admin/sidebar');
            $this->load->view("admin/dashboard");
            $this->load->view('admin/dashboard.php');

        }


    }
    public function registerd()
    {
        $this->load->view('admin/header');
        $this->load->view('admin/registration');
        $this->load->view('admin/footer');

    }

} ?>

1 个答案:

答案 0 :(得分:1)

要获得原始输入,请尝试

// get the raw POST data
$rawData = file_get_contents("php://input");

要进行验证,请尝试

$this->form_validation->set_rules($rawData['email'], 'E-mail', 'required|trim');