WhatsApp Cloud API ile çalışırken, medya dosyaları (resimler, videolar, belgeler ve ses dosyaları) doğrudan gönderilmez. Bunun yerine, WhatsApp bir Media ID sağlar; bu ID’yi kullanarak gerçek dosyayı çekip sunucunuzda saklamanız gerekir.
- Laravel Method (Tavsiye Edilen)
- Core PHP Method
- MIME Type Helper Function
Adım 1: MIME Type’dan Uzantıya Fonksiyonu
Adım 1: MIME Type’dan Uzantıya Fonksiyonu
protected function _get_extension_by_mime($mimeType)
{
$map = [
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/webp' => ,
'video/mp4' => ,
=> ,
=> ,
=> ,
=> ,
=> ,
=> ,
// Daha fazla mime type ekleyin
];
return $map[$mimeType] ?? null;
}
Yöntem 1: Laravel Method (Tavsiye Edilen)
Yöntem 1: Laravel Method (Tavsiye Edilen)
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[])) return false;
$media_url = $response[];
$ext = $this->_get_extension_by_mime([]);
$folder = public_path(); // veya storage_path('/whatsapp_media/')
if (!is_dir()) mkdir(, 0777, true);
$file_name = date() . . uniqid() . time() . . ;
$file_path = . ;
Http::withToken()
->sink()
->get();
return . ;
}
Yöntem 2: Core PHP Method
Yöntem 2: Core PHP Method
function get_media_url(, )
{
if (!) return false;
= curl_init();
curl_setopt_array(, [
CURLOPT_URL => . ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
. ],
]);
= curl_exec();
curl_close();
= json_decode(, true);
if (!isset([])) return false;
= [];
= ->([]);
= . ;
(!()) (, , );
= () '-' uniqid() time() '.' $ext;
= . ;
= $media_url'" -H "Authorization: Bearer '$access_token'" > '$file_path;
// yukarıdaki komutta bir sorun varsa, bunu kullanın
// $command = 'curl -L -s "' . $media_url . '" -H "Authorization: Bearer ' . $access_token . '" > "' . $file_path . '"';
();
'whatsapp_media/' $file_name;
}
Kaynak: Orijinal Makale


