KevinTherrien
KevinTherrien

Reputation: 23

CURL call is working fine directly in CURL but not in PHP 7.3

I want to make a simple call to an API from PHP. It's working fine directly in CURL but not in PHP 7.3.

My real token was replace by TOKEN

CURL:

curl -X POST -H 'Authorization: Bearer TOKEN' -H "Content-Type: application/json" --data-binary '{"query":"{cameraList{name}}"}' https://cloud.camstreamer.com/api/graphql.php

PHP:

<?php

$url = "https://cloud.camstreamer.com/api/graphql.php";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array();
$headers["Authorization"] = "Bearer TOKEN";
$headers["Content-Type"] = "application/json";

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = '{"query": "{cameraList{name}}"}';

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
exit;

Upvotes: 0

Views: 462

Answers (2)

KevinTherrien
KevinTherrien

Reputation: 23

The content type was not sent correctly.

I changed this:

$headers = array();
$headers["Authorization"] = 'Bearer TOKEN';
$headers["Content-Type"] = "application/json";
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

To this:

curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer TOKEN'
));    

Upvotes: 1

You can use

$info = curl_getinfo($ch);
$error = curl_error($ch);

to see what happened.

Upvotes: 1

Related Questions