Justin
Justin

Reputation: 27301

What is the curl_setopt equivalent of exec('curl -k ...')

I am trying duplicate the functionality of this command:

exec('curl -k -d "pameters=here" https://apiurlhere.com', $output);

with curl_exec():

$url =  'https://apiurlhere.com?parameters=here';
$ci = curl_init();
curl_setopt($ci, CURLOPT_POST, TRUE);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ci, CURLOPT_URL, $url);
$response = curl_exec($ci);
curl_close ($ci);
print_r($response);

I have been trying the different options found in the docs (http://php.net/manual/en/function.curl-setopt.php), and thought it was CURLOPT_SSL_VERIFYPEER, but it still failed (response comes back empty).

The URL i'm posting to uses SNI which is causing cURL to fail when the SSL cert returns a different host name than it's expecting.

Is there a curl_setopt equivalent to -k?

Upvotes: 3

Views: 7018

Answers (2)

Bruno
Bruno

Reputation: 122649

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE) is the equivalent of -k on the command line (which you shouldn't really use, since it makes the connection vulnerable to MITM attacks).

Your problem here is that you're assuming that -d "pameters=here" is equivalent to putting these parameters in the URL query like this https://apiurlhere.com?parameters=here.

This isn't the case, see documentation for -d:

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.

You should probably use POSTFIELDS for this:

curl_setopt($ch, CURLOPT_POSTFIELDS, array("parameters" => "here"));

Upvotes: 3

Gilles Quénot
Gilles Quénot

Reputation: 185025

Try that :

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

See http://www.php.net/manual/en/function.curl-setopt.php

Upvotes: 2

Related Questions