3

I want to send http header to Api and get json response.

I have all book detail in books table and I want to get all books.

But i have 5 http header to get access them.

Client-Service,Auth-Key,Content-Type,User-ID,Authorization

Url to get details:

http://127.0.0.1/RestApi/index.php/book/

Controller Code:

    public function index() {
    $method = $_SERVER['REQUEST_METHOD'];
    if ($method != 'GET') {
        json_output(400, array('status' => 400, 'message' => 'Bad request.'));
    } else {
        $check_auth_client = $this->MyModel->check_auth_client();
        if ($check_auth_client == true) {
            $response = $this->MyModel->auth();
            if ($response['status'] == 200) {
                $resp = $this->MyModel->book_all_data();
                json_output($response['status'], $resp);
            }
        }
    }
}

Model Code:

public function book_all_data()
{
    return $this->db->select('id,title,author')->from('books')->order_by('id','desc')->get()->result();
}

I want to access to access on button click but how send http header to rest api page and get all data using codeigniter ?

Manish Tiwari
  • 1,806
  • 10
  • 42
  • 65

1 Answers1

1

Use CURL to set headers. See below example. Hope it will help you

$headers = array(
                 'Client-Service:CLIENT_SERVICE_DETAIL',
                 'Auth-Key:YOUR_AUTH_KEY',
                 'Content-Type:YOUR_CONTENT_TYPE',
                 'User-ID:YOUR_USER_ID',
                 'Authorization:YOUR_AUTHORIZATION',   
                 );

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, YOUR_URL);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_HEADER, 1); 
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);

$buffer = curl_exec($curl_handle);
$header_size = curl_getinfo($curl_handle, CURLINFO_HEADER_SIZE);
$body = substr($buffer, $header_size);

curl_close($curl_handle);
Vinie
  • 2,983
  • 1
  • 18
  • 29
  • And how can i store response from url in variable ? – Manish Tiwari Feb 10 '16 at 10:53
  • i print_r($buffer); then i got HTTP/1.1 200 OK Date: Wed, 10 Feb 2016 10:55:04 GMT Server: Apache/2.4.10 (Unix) OpenSSL/1.0.1j PHP/5.6.3 mod_perl/2.0.8-dev Perl/v5.16.3 X-Powered-By: PHP/5.6.3 Content-Length: 120 Content-Type: application/json; charset=UTF-8 [{"id":"3","title":"Go Programming","author":"Google"},{"id":"1","title":"Codeigniter Rest API","author":"Momo Baruno"}] . I want it only json data only – Manish Tiwari Feb 10 '16 at 10:56