Curl in cakephp

I am trying to connect to an external API with curl but I always get an error;

Here is my code (in a controller) :

$url = "https://api.xxx";

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

$headers = array(
   "Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = <<<DATA
{
"username": "$login",
"password": "$pwd"
}
DATA;

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

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

$response = json_decode($resp);
$token = $response->token;
$this->set('token', $token);

This code works perfectly in a standard php file.
What did I miss ?

I reply to myself :slight_smile:
I need to json_encode data.

I tried http_build_query() but id does not work.

So my solution is :

$fields = array(
            'username' => '$login',
            'password' => '$pwd'
        );
$data = json_encode($fields);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

Hope it helps !

The following code works better than raw curl. Must be wrapper.

use Cake\Http\Client;

$client = new Client(array(
‘headers’ => $headers
));
$postResponse = $client->post($url,json_encode($requestData);

$jsonResponse=$postResponse->getJson();

$getResponse = $client->get($url);

1 Like

Thank you for letting us know of a cake way.
I’ll will try this one when back to work.

Get an error :
2022-11-09 10:05:54 Error: [Cake\Core\Exception\Exception] fopen(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
fopen(): Failed to enable crypto
fopen(https://api.xxx/external/commmande): failed to open stream: operation failed

The code :

$http = new Client([
            'headers' => ["content-type: application/json", 'Authorization' => 'Bearer ' . $token]
        ]);
        $response = $http->post(
            "$url",
            json_encode($data),
            ['type' => 'json']
        );
        $json = $response->json;

this code works in a simple php file :

$url = "https://api.xxx.com/external/commmande";

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

        $headers = array(
            "content-type: application/json",
            "Authorization: Bearer $token",
        );
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        $fields = array(
            "date_commande" => "$date_commande",
            "commande_acceptee" => true,
            "origine_vente" => "ClickAndCollect",
            "email" => "$commande->email",
            "client" => array(
                "nom" => "$commande->last_name",
                "prenom" => "$commande->first_name",
                "telephone" => "$commande->mobile_phone_number",
                "adresse" => "$address",
                "cp" => "$commande->zip_code",
                "ville" => "$commande->city"
            ),
            "id_magasin" => 21,
            "id_magasin_livraison" => 21,
            "id_caisse" => 0,
            "id_tarification" => 0,
            "lieux_conso" => 1,
            "mode_de_faturation" => "TTC",
            "numero_commande" => "$commande->id",
            "date_livraison" => "$date_livraison",
            "commentaire" => "$comment",
            "total_net_ttc" => $total_net_ttc,
            "produits" => $produits,
            "reglements" => array(
                array(
                    'id_reglement_moyen' => 501,
                    'montant_reglement' => $montant_reglement,
                    'date_reglement' => "$date_reglement"
                )
            )
        );
        $data = json_encode($fields);

        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

        $response = curl_exec($curl);
        curl_close($curl);

And so ???