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)