15

I've got this piece of code to launch queries with curl:

function curl_query($full_url, $username, $password, $payload) {
    $additionalHeaders = "";
    $process = curl_init($full_url);
    curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', $additionalHeaders));
    curl_setopt($process, CURLOPT_HEADER, 0);
    curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_POST, 1);
    curl_setopt($process, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($process, CURLOPT_MAXREDIRS, 4);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);
    $return = curl_exec($process);
    if ($return === false) {
        error_log("CURL error ".curl_error($process));
    }
    return $return;
}

The option CURLOPT_SSL_VERIFYPEER is set to false so I can read pages with self-signed certificates. However when I execute this code against an https URL I get an error:

CURL error SSL: certificate subject name 'localhost' does not match target host name '192.168.1.1',

I run this code on a CentOS 5 server with php53 package installed.

Thanks in advance

facha
  • 11,862
  • 14
  • 59
  • 82

2 Answers2

42

Add the option CURLOPT_SSL_VERIFYHOST

curl_setopt($process, CURLOPT_SSL_VERIFYHOST, FALSE);

I realized, that the english version of the PHP documentation missing this important information:

(translated from the german version of the PHP manual)

CURLOPT_SSL_VERIFYHOST has to be set to true or false if CURLOPT_SSL_VERIFYPEER has been deactivated.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    How obvious. There's both a `CURLOPT_SSL_VERIFYHOST` **and** a `CURLOPT_SSL_VERIFYPEER`. I missed that; this saved me. Thanks! – Dan Nissenbaum Aug 19 '15 at 01:05
1
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($process,CURLOPT_SSL_VERIFYHOST, FALSE);

ADD THIS TO LINES WILL HELP

conrad wawire
  • 194
  • 1
  • 5