簡體   English   中英

如何將文件(pdf)從本地計算機發送到 API Whatsapp(聊天 API)

[英]How to Send File(pdf) from Local Computer to API Whatsapp (Chat-API)

偉大的,

今天我想做一些任務來使用 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;
?>

如果我將文件更改為在線 url 可以正常工作,但是當使用本地文件時,我得到這樣的結果錯誤

{"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?

太感謝了

我知道這個問題是很久以前發布的,但如果有人正在尋找解決方案,我會留下答案。

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

為了:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM