I need to access a RESTful webservice from PHP (only GET for now). The service can only be accessed over HTTPS with a valid client certificate.
I found plenty of basic auth examples for PHP, but not a single one for client-side cert-based HTTP auth. Is there a PHP HTTP client which can also send certificates to the server?
For now I am using an external application (wget), but this is rather slow and hacky.
Certificate-based authentication is not part of HTTP but part of SSL/TLS.
You can use cURL to do such authentication:
$ch = curl_init('https://example.com/');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, '1');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, '1');
curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cert/ca.crt');
curl_setopt($ch, CURLOPT_SSLCERT, '/path/to/cert/client-cert.pem');
$response = curl_exec();
curl_close($ch);
See the manual page of curl_setopt
for more information on the options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With