4
votes

I use this PHP function below to use curl to contact an outside API

function api_post($url, $data = array()) {

global $api_key;
global $password;

$data = json_encode($data);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json'
));

curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':' . $password);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

$response = curl_exec($ch);

return $response;   
}

The api that I m communicating with is about to insist on using tls v1.2 which is a good thing except for some reason my code is using version 1.0.

It is fine if I do it from my local server but on the production server (An Amazon Web Services EC2 instance on AWS Elastic Beanstalk) it is not. I guess it has something to do with my server setup but I have no idea what or how to fix it.

Here is the curl section from my PHPinfo. Maybe I need to upgrade it or something? But how would I do this?

enter image description here

2

2 Answers

0
votes

To enforce a TLS version on curl, you may need to use

bool curl_setopt ( resource $ch , int $option , mixed $value )

as documented here: http://php.net/manual/en/function.curl-setopt.php

example for TLS v1.2

curl_setopt ($setuploginurl, CURLOPT_SSLVERSION, 6); 

CURLOPT_SSLVERSION: One of CURL_SSLVERSION_DEFAULT (0), CURL_SSLVERSION_TLSv1 (1), CURL_SSLVERSION_SSLv2 (2), CURL_SSLVERSION_SSLv3 (3), CURL_SSLVERSION_TLSv1_0 (4), CURL_SSLVERSION_TLSv1_1 (5) or CURL_SSLVERSION_TLSv1_2 (6).

0
votes

OK, so I figured it out and thought I'd put the answer up here in case it is useful to anyone else.

As I suspected the problem was the curl version. In order for the line which tells curl which version to use to take affect I needed to be on curl version 7.34 or higher.

curl_setopt($ch, CURLOPT_SSLVERSION, 6);

So how do you upgrade the curl version? Well there was a big upgrade button on the environment's main page to upgrade the version of Linux running on my instances so I clicked on that it upgraded curl at the same time. I now have curl version 7.38 and its using TLS v1.2 as I wanted.