php - How to get/set Header in Rest Server API? -
i have chriskacerguis rest server ,that listen client request api server do. base on client request want send/response data client in header only.
my questions are:
how access client header first?
then
how set header in rest server?
this how send request rest server:
function request_curl($url = null) { $utc = time(); $post = "id=1&customerid=1&amount=2450&operatorname=jondoe&operator=12"; $header_data = array( "content-type: application/json", "accept: application/json", "x-api-key:3ecbcb4e62a00d2bc58080218a4376f24a8079e1", "x-utc:" . $utc, ); $ch = curl_init(); $curlopts = array( curlopt_url => 'http://domain.com/customapi/api/clientrequest', curlopt_returntransfer => true, curlopt_httpheader => $header_data, curlopt_followlocation => true, curlopt_post => true, curlopt_postfields => $post, curlopt_header => 1, ); curl_setopt_array($ch, $curlopts); $answer = curl_exec($ch); // if there error, show if (curl_error($ch)) { die(curl_error($ch)); } curl_close($ch); echo '<pre>'; print_r($answer); echo '</pre>'; }
below rest server function listen request , response header:
public function clientrequest_post() { // getting post data $entitybody = file_get_contents('php://input', 'r'); $this->response($entitybody,200); //getting header data ,no idea }
may try php function getallheaders() fetch header data you. if want convert array, use foreach.
so header data , convert array
$headers=array(); foreach (getallheaders() $name => $value) { $headers[$name] = $value; }
now if want body , convert array well
$entitybody = file_get_contents('php://input', 'r'); parse_str($entitybody , $post_data);
the final function this...
public function clientrequest_post() { $headers=array(); foreach (getallheaders() $name => $value) { $headers[$name] = $value; } $entitybody = file_get_contents('php://input', 'r'); parse_str($entitybody , $post_data); $this->response($entitybody, 200); }
btw, assume $this->response($entitybody,200);
generate response you. best of luck it
Comments
Post a Comment