Most of the Web/API services providers are shifting their environments to TLS 1.2 or greater. So to consume their services via PHP applications, you also need to force your application to use TLS 1.2 during making a connection. This tutorial will help you, how to use TLS 1.2 with PHP cURL.
Using TLS 1.2 with PHP CURL Forcefully
You can add the following code to your curl requests to use TLS 1.2. Use 6 as the value of
CURLOPT_SSLVERSION forces cURL to use TLS 1.2.
Below is the sample code to force use tls 1.2 with php curl:
curl_setopt ($ch, CURLOPT_SSLVERSION, 6);
For the example, I am using a sample script from our another articlesubmitting JSON data with cURL and PHP. In that script, we will add code to forece use of tls 1.2.
Below is the sample script:
// A sample PHP Script to POST data using cURL
// Data in JSON format
$data = array(
'username' => 'tecadmin',
'password' => '012345678'
$payload = json_encode($data);
$ch = curl_init('https://api.example.com/api/1.0/user/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt ($ch, CURLOPT_SSLVERSION, 6); //Force requsts to use TLS 1.2
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Length: ' . strlen($payload))
$result = curl_exec($ch);
You can execute above script in webbrowser or from the command line interface.
In this tutorial, you have learned to use tls 1.2 with PHP/cURL forcefully.