You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用WhatsApp API发送文档时如何设置文件名?

问题

我尝试通过WhatsApp API发送pdf、docx、ppt等格式的文档,按照官方文档编写了如下代码,但接收端收到的文档显示名为null.pdf,请问如何设置才能让接收端收到带有原始文件名的文档?

$client = new Client();
$headers = [
    'Authorization' => 'Bearer '.config('app.wa_token'),
    'Content-Type' => 'application/json'
];
$body = '{
    "messaging_product": "whatsapp",
    "recipient_type": "individual",
    "to": "'.$receiver.'",
    "type": "document",
    "document": {
        "link": "'.$link_docx.'",
        "caption": "'.$filename.'"
    }
}';

$request = new Request('POST', 'https://graph.facebook.com/v13.0/'.$kod.'/messages', $headers, $body);
$res = $client->sendAsync($request)->wait();
return $res->getBody();
解决方案

问题出在你仅设置了caption字段——这个字段只是文档下方的说明文字,并不会被作为文件名识别。WhatsApp API需要通过document对象下的filename字段来指定文档的原始文件名。

修改请求体,在document节点中添加filename字段,值设置为包含完整扩展名的原始文件名(比如年度报告.pdf、项目方案.docx):

$client = new Client();
$headers = [
    'Authorization' => 'Bearer '.config('app.wa_token'),
    'Content-Type' => 'application/json'
];
// 注意添加了filename字段,使用包含扩展名的原始文件名
$body = '{
    "messaging_product": "whatsapp",
    "recipient_type": "individual",
    "to": "'.$receiver.'",
    "type": "document",
    "document": {
        "link": "'.$link_docx.'",
        "caption": "'.$filename.'",
        "filename": "'.$original_full_filename.'"
    }
}';

$request = new Request('POST', 'https://graph.facebook.com/v13.0/'.$kod.'/messages', $headers, $body);
$res = $client->sendAsync($request)->wait();
return $res->getBody();

注意事项

  • filename字段必须包含完整的文件名和对应的扩展名(如.pdf、.docx),这样接收端才能正确识别文档格式并显示正确的文件名
  • 确保$original_full_filename变量存储的是正确的原始文件名(可以从文件URL中解析,或者从本地文件元数据中获取)

内容的提问来源于stack exchange,提问作者Blue Moon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 12:36:23