0

This is my sample JSON Request

{
    "Username":"admin",
    "Password":"root123",
   "PinCode” : "hello321"
}

And also I want to post a request token as well, the request token has to be posted as a Request Header.

When I successfully sent those data and valid token, I need to receive a jSON response as below,

{
            "Title": "Mr.",
            "Name": "Pushkov",
            "Age": "18"

}   

My Question is,

How can I POST my above mentioned JSON data and the token in PHP cURL and display an error if the details are wrong in code igniter?

This is what I have done so far..and its not giving me the exact output im looking for and I couldn't figure out a way to send my token as a header request...

public function send_veri(){

$url='http://helloworld21.azurewebsites.net/api/member/login';

$ch = curl_init('http://localhost/apphome');


$data = array("Username" =>$this->input->post('un'), "Password"  =>$this->input->post('pw'), "PinCode" =>$this->input->post('VerificationCode'));                                                                    
$data_string = json_encode($data);   


// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
); 
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Set the url
//curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
}

once I run this, I could only get Username, Password and the PinCode only. I cannot post the request token in my header, in causing of that i cannot get the member detail response.

  • You may have a look at [curl_error](http://php.net/manual/de/function.curl-error.php) and https://stackoverflow.com/questions/12331224/how-to-include-authorization-header-in-curl-post-http-request-in-php – BenRoob Nov 11 '17 at 08:51

1 Answers1

0

For Sending Data -

  public function send_veri(){
      $url='http://helloworld21.azurewebsites.net/api/member/login';
      $ch = curl_init( $url );
      $bodyData=json_encode(array()); // here you send body Data
      $data = json_encode(array("Username" =>$this->input->post('un'), "Password" =>$this->input->post('pw'), "PinCode" =>$this->input->post('VerificationCode')));

                    curl_setopt( $ch, CURLOPT_POSTFIELDS, $bodyData );

                    curl_setopt( $ch, CURLOPT_HTTPHEADER,  array("headerdata: ".$data.",Content-Type:application/json"));

                    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

                    $result = curl_exec($ch);

                    curl_close($ch);
                   }

Get Header data on Apache Server Code -

            $hEAdERS = apache_request_headers();
            $data = json_decode($hEAdERS['headerdata'], true);  
            echo $contentType=$hEAdERS['Content-Type'];
            echo $Username =  @trim($data['Username']);
            echo $Password =  @trim($data['Password']);
            echo $PinCode =  @trim($data['PinCode']);

It works for me.

Krishan Kumar
  • 201
  • 4
  • 8
  • but how can i post my request token as a header request? as an example, if my header name is _header and value is "AsdRgfgfvd" how can I post that? –  Nov 11 '17 at 09:38
  • Add your header other parameter here - `curl_setopt( $ch, CURLOPT_HTTPHEADER, array("headerdata: ".$data.",Content-Type:application/json"));` – Krishan Kumar Nov 11 '17 at 09:48
  • for exeample - 'curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Authorization-Key: 5996880420','headerdata: '.$data', 'Content-Type:application/json'));' – Krishan Kumar Nov 11 '17 at 09:50
  • **can you please accept my answer if it solved your issue?** – Krishan Kumar Nov 11 '17 at 10:08
  • which is your server Apache OR Nignx? – Krishan Kumar Nov 11 '17 at 10:34
  • This is what i really want to do. in the first window user enters the username and the password. if those credentials are correct then the user have to enter the pin code. once the pin is correct request token will be generated. I want to post all 3 credentials(UN,PW and pin) as jason data and given request token should be posted as header request.. whrn the all 4 are corect user will get a json response which contains his profile info.. –  Nov 11 '17 at 11:00
  • you can use session or transfer the one form information to another form – Krishan Kumar Nov 13 '17 at 05:08