Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Images using Whatsapp Cloud Api

Tags:

php

whatsapp

I am trying to send images with Whatsapp Cloud API. Using PHP, I am able to send normal text messages successfully.

When going through the docs, what does 'MEDIA_OBJECT_ID' mean ? An example would be great.

curl -X  POST \
 'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages' \
 -H 'Authorization: Bearer ACCESS_TOKEN' \
 -d '{
  "messaging_product": "whatsapp",
  "recipient_type": "individual",
  "to": "PHONE_NUMBER",
  "type": "image",
  "image": {
    "id" : "MEDIA_OBJECT_ID"
  }
}'

thanks

like image 448
Yoosu Avatar asked Jun 08 '26 23:06

Yoosu


2 Answers

First, you need to upload the media file to the WhatsApp Server then WhatsApp will respond back the MEDIA_OBJECT_ID

$target="/home/rishabh/uploads/myImage.png";
$mime=mime_content_type('myImage.png')

 $file = new CURLFILE($target);
 $file->setMimeType($mime);
 $curl = curl_init();
 curl_setopt_array($curl, array(
    CURLOPT_URL => "https://graph.facebook.com/v13.0/$phoneSid/media",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => array("messaging_product" => "whatsapp", "type"=>$mime, "file"=> $file),
    CURLOPT_HTTPHEADER => array(
         "Authorization: Bearer ".$whatsappToken
     ),
));
$resultWhatsAppMedia = json_decode(curl_exec($curl), true);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

$MEDIA_OBJECT_ID = $resultWhatsAppMedia['id']; //MEDIA OBJECT ID

Now, you will get the media object id. You need to send the media from API

$FileName="Caption Name or Image Name";
$messageBody = [
                "messaging_product"=>"whatsapp",
                "recipient_type" => "individual",
                "to" => "$to_number",
                "type" => "image",
                "image" => [
                    "id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID
                    "caption" => $FileName,
                ]
            ];

$curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => json_encode($messageBody),
        CURLOPT_HTTPHEADER => array(
            "Authorization:Bearer $YOUR_WHATSAPP_ACCESS_TOKEN",
            'Content-Type: application/json'
        ),
    ));
    $response = json_decode(curl_exec($curl), true);
    $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);

Here is the different type of media JSON

 // for mp3
     $messageBody = [
                    "messaging_product"=>"whatsapp", 
                    "recipient_type" => "individual",
                    "to" => "$to_number",
                    "type" => "audio",
                    "audio" => [
                        "id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID
                    ]
                ];
    
    // for pdf, doc, apk etc
      $messageBody = [
                    "messaging_product"=>"whatsapp",
                    "recipient_type" => "individual",
                    "to" => "$to_number",
                    "type" => "document",
                    "document" => [
                        "id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID,
                        "caption" => $fileName,
                        "filename" => $fileName,
                    ]
                ];
    
     // for mp4
     $messageBody = [
                    "messaging_product"=>"whatsapp",
                    "recipient_type" => "individual",
                    "to" => "$to_number",
                    "type" => "video",
                    "video" => [
                       "id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID,
                        "caption" => $media['fileName'],
                    ]
                ];
    
    $RequestJSON = json_encode($messageBody)
like image 145
Rishabh Rawat Avatar answered Jun 11 '26 13:06

Rishabh Rawat


You need to upload the media to https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/media

The response will give you the "MEDIA_OBJECT_ID"

OR

use image link instead

curl -X  POST \
 'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages' \
 -H 'Authorization: Bearer ACCESS_TOKEN' \
 -d '{
  "messaging_product": "whatsapp",
  "recipient_type": "individual",
  "to": "PHONE_NUMBER",
  "type": "image",
  "image": {
    "link" : "Image URL"
  }
}'
like image 28
Whebcraft Avatar answered Jun 11 '26 12:06

Whebcraft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!