Skip to content
Advertisement

Logging into a soap API with cURL and Laravel

I’m having some trouble with this. I am trying to login to a Soap API with SSL I have generated the .pem file and can verify it is good by doing the following

curl -v --cert mycertfile.pem --pass mypassword https://someurl.com:9443/login/

The following give me HTTP/1.1 202 Accepted

Now, when I essentially try to do the same thing within a laravel controller with the following, I get the following error “curl error: error:14094412:SSL routines:ssl3_read_bytes:sslv3 alert bad certificate”

Here is the code in my controller

$apiKey = 'mypassword';
$file = File::get(storage_path('app/certs/mycertfile.pem'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://someurl:9443/login/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('AUTH-KEY: ' . $apiKey));
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, 'mypassword');
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, $file);
$rawBody = curl_exec($ch);
if($rawBody === false)
{
  $err = 'curl error: ' . curl_error($ch);
  curl_close($ch);
  echo $err;
  return $err;
}
else
{
  echo "response ==";
  echo $rawBody;
  echo "execu";
  curl_close($ch);
  return 'Operation completed without any errors';
}

Any ideas why using curl from the command line using the same key, same computer, URL and .pem file is OK, where as using cURL in laravel, gives this error? Interestingly, I am sure this used to work, but have since upgraded to PHP8 and a few other things I think. Perhaps that is why, but I can’t see anything mentioning issues with PHP8.

Advertisement

Answer

After a bit of research, I was able to get the above working by using the following.

$client = new SoapClient('location of wsdl file.wsdl', array(
                    'local_cert' => 'mycertfile.pem',
                    'passphrase' => 'mypassword',
                    // 'location' => 'https://webserviceurl/login/' 
                )
);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement