Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php curl: SSL_VERIFYPEER option doesn't have effect

Tags:

php

curl

ssl

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

like image 485
facha Avatar asked Apr 25 '13 16:04

facha


People also ask

Why PHP cURL is not working?

Solution: If when you login to your CPanel you can verify that the cURL support is enabled but it is not working then your server IP might be banned. If it isn't please re-check the firewall configuration with your server, as it might an issue there that is causing the problem.

How does cURL verify SSL certificate?

libcurl performs peer SSL certificate verification by default. This is done by using a CA certificate store that the SSL library can use to make sure the peer's server certificate is valid.

Which function in PHP do you use to set cURL options?

Parameters ¶ A cURL handle returned by curl_init(). The CURLOPT_XXX option to set. The value to be set on option .

Does cURL work in PHP?

cURL is a PHP library and command-line tool (similar to wget) that allows you to send and receive files over HTTP and FTP. You can use proxies, pass data over SSL connections, set cookies, and even get files that are protected by a login.


2 Answers

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.

like image 200
hek2mgl Avatar answered Oct 05 '22 22:10

hek2mgl


curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($process,CURLOPT_SSL_VERIFYHOST, FALSE);

ADD THIS TO LINES WILL HELP

like image 25
conrad wawire Avatar answered Oct 05 '22 23:10

conrad wawire