Home > Blockchain >  Whatsapp business - POST Media
Whatsapp business - POST Media

Time:08-30

I'm trying to do a POST using the Facebook Graph API. I did follow the documentation. I also separate the post fields as: file, type and messaging_product: same result.

$this->endpoint = 'PHONE_NUMBER_ID/media';
$this->config->default_access_token = 'XXXXXXXX';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v14.0/'.$this->endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
    'file' => '@/var/www/html/6218062.pdf;type=application/pdf',
    'messaging_product' => 'whatsapp'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

$headers = array();
$headers[] = 'Authorization: Bearer '.$this->config->default_access_token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

print_r($result);

This is the result:

{"error":{"message":"An unknown error has occurred.","type":"OAuthException","code":1,"fbtrace_id":"A_A8o81x3K5kCpGD5cP1rw6"}}

I can send messages and everyting, but I can not send message with documents from local server, only from external server, for example: https://sitename.com/file/doc.pdf.

I want to use files from my local server and send it as a message.

Can anyone help me?

CodePudding user response:

I solved the issue.

Here is the working code.

$this->endpoint = 'PHONE_NUMBER_ID/media';
$this->config->default_access_token = 'XXXXXXXX';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v14.0/'.$this->endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
    'file' => curl_file_create('/var/www/html/6218062.pdf', 'application/pdf'),
    'messaging_product' => 'whatsapp'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$headers = array();
$headers[] = 'Authorization: Bearer '.$this->config->default_access_token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

print_r($result);
  • Related