Home > Back-end >  Convert Faceboook Meta cUrl to PHP
Convert Faceboook Meta cUrl to PHP

Time:06-30

How can I convert the following cUrl to PHP.

curl -i -X POST \
https://graph.facebook.com/v12.0/FROM_PHONE_NUMBER_ID/messages \
      -H 'Authorization: Bearer ACCESS_TOKEN' \
      -H 'Content-Type: application/json' \
      -d '{ "messaging_product": "whatsapp", "to": "TO_PHONE_NUMBER", "type": "template", "template": { "name": "hello_world", "language": { "code": "en_US" } } }'

I want to use the standard curl_init and curl_exec to execute the url function.

So far, I have tried the following.

$endpoint = "https://graph.facebook.com/v12.0/".$whatsappPhoneNumberId."/messages";
            
            $headers = array();
            $headers[] = 'Content-Type: application/json';

            $bodyFields = [
                "messaging_product" => "whatsapp",
                "to" => "XXXXXXXXXXX",
                "type" => "template",
                "template" => [
                    "name" => "hello_world",
                    "languauge" => [
                        "code" => 'en_US',
                    ]
                ],
            ];

            $cUrl = curl_init($endpoint);
            curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($cUrl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($cUrl, CURLOPT_POSTFIELDS, json_encode($bodyFields));
            
            $response = curl_exec($cUrl);
            curl_close($cUrl);

I need to add the access token, and also getting an error as follows when I call the endpoint

{"error":{"message":"(#100) Unexpected key \"languauge\" on param \"template\".","type":"OAuthException","code":100 }}

CodePudding user response:

To add access token you should simply add another header:

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer ' . $accessToken;

also getting an error as follows when I call the endpoint - spelling error in word languauge. Should be language

  • Related