DEV Community

Ankit Verma
Ankit Verma

Posted on

Download and Save WhatsApp Media Using Media ID (WhatsApp Cloud API) in Laravel and Core PHP

When working with the WhatsApp Cloud API, media files such as
images, videos, documents, and audio are not sent directly. Instead,
WhatsApp provides a Media ID, which you must use to fetch the actual
file and store it on your server.

  • Laravel Method (Recommended)
  • Core PHP Method
  • MIME Type Helper Function

Step 1: MIME Type to Extension Function

protected function _get_extension_by_mime($mimeType)
{
    $map = [
        'image/jpeg' => 'jpg',
        'image/png' => 'png',
        'image/webp' => 'webp',
        'video/mp4' => 'mp4',
        'video/3gpp' => '3gp',
        'audio/mpeg' => 'mp3',
        'audio/ogg' => 'ogg',
        'audio/amr' => 'amr',
        'application/pdf' => 'pdf',
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
        // Add more mime types as needed
    ];

    return $map[$mimeType] ?? null;
}
Enter fullscreen mode Exit fullscreen mode

Method 1: Laravel Method (Recommended)

use Illuminate\Support\Facades\Http;

public function downloadWhatsAppMedia($media_id, $access_token)
{
    if (!$media_id) return false;

    $url = "https://graph.facebook.com/v18.0/" . $media_id;

    $response = Http::withToken($access_token)->get($url);

    $response = json_decode($response->body(), true);

    if (!isset($response['url'])) return false;

    $media_url = $response['url'];

    $ext = $this->_get_extension_by_mime($response['mime_type']);

    $folder = public_path("/whatsapp_media/"); // or storage_path("/whatsapp_media/")

    if (!is_dir($folder)) mkdir($folder, 0777, true);

    $file_name = date('Ymd') . '-' . uniqid() . time() . '.' . $ext;

    $file_path = $folder . $file_name;

    Http::withToken($access_token)
        ->sink($file_path)
        ->get($media_url);

    return "/whatsapp_media/" . $file_name;
}
Enter fullscreen mode Exit fullscreen mode

Method 2: Core PHP Method

function get_media_url($media_id, $access_token)
{
    if (!$media_id) return false;

    $curl = curl_init();

    curl_setopt_array($curl, [
        CURLOPT_URL => "https://graph.facebook.com/v18.0/" . $media_id,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            "Authorization: Bearer " . $access_token
        ],
    ]);

    $response = curl_exec($curl);

    curl_close($curl);

    $response = json_decode($response, true);

    if (!isset($response['url'])) return false;

    $media_url = $response['url'];

    $ext = $this->_get_extension_by_mime($response['mime_type']);

    $folder = __DIR__ . "/whatsapp_media/";

    if (!is_dir($folder)) mkdir($folder, 0777, true);

    $file_name = date('Ymd') . '-' . uniqid() . time() . '.' . $ext;

    $file_path = $folder . $file_name;

    $command = 'curl -k -L -s "'.$media_url.'" -H "Authorization: Bearer '.$access_token.'" > '.$file_path;

    // if any issue with above command, use this
    // $command = 'curl -L -s "' . $media_url . '" -H "Authorization: Bearer ' . $access_token . '" > "' . $file_path . '"';

    shell_exec($command);

    return "whatsapp_media/" . $file_name;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)