Accessing the REST Server Using the Requests HTTP Library

Home

Tests Home

Code Example:

        $user_id = 1;

        $this->load->helper('url');

        $headers = array('Accept' => 'application/json');
        $options = array(
            'auth' => array('admin', '1234'),
            'verify' => false,  // Disable SSL verification, this option value is insecure and should be avoided!
        );
        $request = Requests::get(site_url('api/example/users/id/'.$user_id.'/format/json'), $headers, $options);

        $result = $request->body;
        $status_code = $request->status_code;
        $content_type = $request->headers['content-type'];

Result:

string(56) "{
    "status": false,
    "error": "Invalid API key "
}"

Status Code:

403

Content Type:

application/json; charset=utf-8