如何从本地计算机发送文件(pdf)到 API Whatsapp (Chat-API)

问题描述 投票:0回答:1

问候,

今天我想做一些任务,使用 Chat-API Whatsapp 上传一些 pdf 文件。 该任务也许我会在 Windows shell 上运行,但现在我在 PHP 文件上测试它。 我使用这样的文档

$to = 'myPhone';
$url = 'https://chat-apiurl?andtokenhere';
$imageLocation = 'http://localhost/chat-api/file.pdf';
$data = [
    'phone'=> $to,
    'body' =>$imageLocation,
    'filename'=>"filepdf.jpg",
    'caption'=>'test',
];
$send = json_encode($data);

$options = stream_context_create(['http' => [
        'method'  => 'POST',
        'header'  => 'Content-type: application/json',
        'content' => $send,
    ]
]);
// Send a request

$result = file_get_contents($url, false, $options);
echo $result;
?>

如果我将文件更改为在线网址,那就可以正常工作,但是当使用本地文件时,我会得到这样的结果错误

{"error":"Unsupported file type"}

我尝试将文件转换为 Base 64,但返回的错误如下

{"sent":false,"message":"Message was not sent: empty body. Please provide message text in body parameter in JSON POST."}

这就是我转换为base64的方式

$getExtension = explode(".",$imageLocation);
$base64 = 'data:image/'.$getExtension[1].';base64,'.file_get_contents($imageLocation);

有人可以帮我发送本地文件吗?或者也许有其他方法可以做到这一点。或者其他API?

非常感谢你

php whatsapp
1个回答
0
投票

我知道这个问题很久以前就已经发布了,但我会留下答案,以防有人正在寻找解决方案。

$base64='data:image/'.$getExtension[1].';base64,'.file_get_contents($imageLocation);

对于:

$base64='data:image/'.$getExtension[1].';base64,'.base64_encode(file_get_contents($imageLocation));
© www.soinside.com 2019 - 2024. All rights reserved.