• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
set up rest with https

#1
I have set up REST server support https using my own generated certificate.

Then I have the client POSTing data using https. But it seems not work. When using http it works. Here the code

PHP Code:
case "POST"// Used to create a new object on the server (INSERT)
    if($data === NULL){
        return FALSE;
    } else {
       $fields_string "";
       foreach($data as $key=>$value){
            $fields_string .= $key.'='.$value.'&';
       }
       $fields_string rtrim($fields_string,'&');
       $ch curl_init($url);
       curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
       curl_setopt($ch,CURLOPT_POST,count($data));
       curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
       if($scheme == 'https'){
            curl_setopt($chCURLOPT_SSL_VERIFYPEERTRUE);
            curl_setopt($chCURLOPT_SSL_VERIFYHOST2);
            $cert config_item('cert_path');
            curl_setopt($chCURLOPT_CAINFO$cert);
       }
       $rdata curl_exec($ch);
       $this->responseCode curl_getinfo($ch,CURLINFO_RESPONSE_CODE);
       curl_close($ch);
   }
   break; 

I'm sure config_item('cert_path') points to certificate file downloaded from server

Tell me if I'm wrong setting up curl.
Also, how to know if my clients certificate rejected by server? Is CI reports it in log (server) or log (client)?
Reply

#2
make a var_dump on $rdata for error messages.
cert_path should be a local path to a .pem file.
Reply

#3
i do log_message('debug', var_export($rdata, TRUE));
The result is 'false'
No messages Sad
Reply

#4
Ok, you got an error then.

Use curl_error to see what:
http://php.net/manual/en/function.curl-e...r-examples
Reply

#5
OK, done. Thanks..
So is use curl_error($ch) to see.
I'm using CI4, so i set cert path in .env : app.CertPath = APPPATH.'Cert/livehost.crt'
and in the code : curl_setopt($ch, CURLOPT_CAINFO, $config->CertPath);
But unfortunately APPPATH not work on .env,
curl error says "error setting certificate verify locations" and show plain APPPATH !!
It should be changed with 'something/application'

So I change on .env : app.CertPath = 'application/Cert/livehost.crt'
and in the code :
curl_setopt($ch, CURLOPT_CAINFO, ROOTPATH.$config->CertPath);
Reply

#6
I haven't tried CI4, so can't give you any feedback on why that's not working. But at least you can hardcode it for now.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.