PHP Curl failed to REST API

Hi guys,

I try to call service from PHP with this script:

$ch   = curl_init("https://XXXXXX:8123/api/services/tts/google_say");
$data = array(
    'entity_id' => 'media_player.alfa',
    'message'   => 'LOREM IPSUM DOLOR',
);
$payload = json_encode($data);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "Content-Type: application/json",
));

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// $output contains the output string
$output   = curl_exec($ch);
$httpcode = curl_getinfo($ch);

curl_close($ch);
var_dump($httpcode);

I dont have any erorrs. This is the debug print:

{ ["url"]=> string(57) "https://xxxxxxxxxx:8123/api/services/tts/google_say" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(20) ["redirect_count"]=> int(0) ["total_time"]=> float(0.050192) ["namelookup_time"]=> float(0.000419) ["connect_time"]=> float(0.013203) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(11) "xx.xx.xx.xx" ["certinfo"]=> array(0) { } ["primary_port"]=> int(8123) ["local_ip"]=> string(13) "192.168.1.100" ["local_port"]=> int(53815) }

But if i execute curl cli command it works:

curl -X POST -H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxx" -H "Content-Type: application/json" -d "{\"message\": \"LOREM IPSUM DOLOR\", \"entity_id\": \"media_player.alfa\"}" https://xxxxxxxxx:8123/api/services/tts/google_say

Any ideas?

Yes, you are using -X , to ignore invalid certificate…
You should also use that in your curl… In your curlopt…

Much thanks i set the flags to disable ssl verification and works like a charm

curl_setopt($cHandler, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($cHandler, CURLOPT_SSL_VERIFYPEER, false);

It strange. I use a Lets Encrypt Certificate. Cheking in https://www.sslshopper.com/ and its valid

Yeah, ok my case too… Not sure what’s the root cause