简体   繁体   中英

Whatsapp Cloud Api get Media Object ID

I want to upload the media via Whatsapp cloud API in order to send the media to WhatsApp, I have followed the link below, but having the stated issue.

https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#get-media-id

Array ( [error] => Array ( [message] => (#100) The parameter messaging_product is required. [type] => OAuthException [code] => 100 [fbtrace_id] => AQPftb9Bf343375gQ_QytxNen ) ) 400

My Code is


// @1640191015925.jpg on my root folder
$post = [
    'file' => '@1640191015925.jpg;type=image/jpeg',
    'messaging_product'   => 'whatsapp'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v13.0/Phone-Number-ID/media');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$YOUR_WHATSAPP_ACCESS_TOKEN = '+++++++';
$headers[] = "Authorization: Bearer $YOUR_WHATSAPP_ACCESS_TOKEN";
$headers[] = "Content-Type: image/jpeg";


curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = json_decode(curl_exec($ch), true);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

//$MEDIA_OBJECT_ID = $result['media'][0]['id']; //MEDIA OBJECT ID
print_r($result);
print_r($httpcode);

?> ```




I had the same issue, after some digging around I wrote this function to upload a file and return the id.

function waFileUploader($token, $file, $senderid){
  $mime = mime_content_type($file);
  $info = pathinfo($file);
  $name = $info['basename'];
  $curlfile = new CURLFile($file, $mime, $name);
  $filedata = array("messaging_product"=>"whatsapp", "file" => $curlfile);
  $url = 'https://graph.facebook.com/v13.0/' . $senderid . '/media';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER,array("Authorization: Bearer $token", "Content-Type: multipart/form-data"));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
  $r = curl_exec($ch);
  curl_close($ch);
  $id = json_decode($r, true);
  return $id["id"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM