Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting cURL to work in PHP with HTTPS

Tags:

php

curl

I have the following code which works in test regions which do not use SSL, but not on the production system, which does. The call itself works in a browser, but when running it via cURL, I get a 500 error.

$region = "https://api.mysite.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . "/cacert.pem");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_URL, $region . $api);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$resp = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch); 

return $resp; 

If I don't put in CURL_OPT_FAILONERROR, then I don't get any error, just a blank response. I'm pretty sure this has to do with the fact that this is via https (as that is the only difference between my test region and my current region), but I can't figure out how to get this to work.

like image 659
Elie Avatar asked Dec 04 '25 07:12

Elie


1 Answers

To confirm the first issue is due to ssl or not, check with following

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Does it work with that ?

like image 50
user1423458 Avatar answered Dec 05 '25 21:12

user1423458