Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform cert-based auth with a PHP HTTP client

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.

like image 885
user647790 Avatar asked Mar 07 '11 08:03

user647790


1 Answers

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.

like image 95
Gumbo Avatar answered Oct 13 '22 21:10

Gumbo