Clickatel WhatsApp集成无法发送图像

发布于 2025-01-26 16:47:27 字数 906 浏览 2 评论 0原文

我无法使用ClickAtel One API发送媒体文件,尤其是图像(JPEG)。

但是,正在发送短信。

以下是我的代码段以发送文件:( php)

$header = [
    'Authorization: ' . $clickatel_api_key,
    'Content-Type: image/jpeg'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, 'https://platform.clickatell.com/v1/media?fileName=' . $name . '&to=254712345678');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

return $result;

以下是请求的响应:

{"error":null,"fileId":"2eee5d0eee4fc4f42943e47c06f12345fdss2ddd.jpg","accepted":true}

未交付媒体文件。

I am unable to send media files especially images(jpeg) using the Clickatel One API.

However, text messages are being delivered.

Below is my code snippet for sending the file: (PHP)

$header = [
    'Authorization: ' . $clickatel_api_key,
    'Content-Type: image/jpeg'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, 'https://platform.clickatell.com/v1/media?fileName=' . $name . '&to=254712345678');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

return $result;

Below is the response from the request:

{"error":null,"fileId":"2eee5d0eee4fc4f42943e47c06f12345fdss2ddd.jpg","accepted":true}

The media file is not delivered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里兽 2025-02-02 16:47:27

我认为您在示例中使用的API调用您上传文件并返回文件ID。接下来,您必须使用该文件ID发送一条消息(另一个HTTP帖子),示例:

{
  "messages": [
    {
      "channel": "whatsapp",
      "to": "2799900001",
      "media": {
        "fileId": "USE YOUR FILE ID HERE",
        "caption": "First Image File"
      }
    }
  ]
}

上传的媒体文件将用于上传后30天发送

的消息。

替代方案是执行一个http请求,其中包含文件数据内联的文件:

{
  "messages": [
    {
      "channel": "whatsapp",
      "to": "2799900001",
      "content": "/9j/4AAQSkZJRgABAQEASABIAAD/2w...SDayT2Nha/OIUS3FhlyHzB8ic6ctekf/9k=",
      "media": {
        "contentType": "image/png",
        "caption": "First Image File"
      }
    }
  ]
}

我所指的Clickatell文档中的示例称为:

  • WhatsApp:whatsapp:通过参考媒体:一个消息:一条消息
  • WhatsApp:Inline Media:一条消息,

如果您使用通过参考发送的方法(涉及两个HTTP请求),则在所有HTTP请求之间重复使用您的curl对象(甚至对于其他消息),以便您重复使用将是有益的您的HTTP连接可降低延迟,并减少您身边的CPU使用情况。

I think the API call you do in your example uploads a file and returns a file ID. Next you must send a message (another HTTP POST) using that file ID, example:

{
  "messages": [
    {
      "channel": "whatsapp",
      "to": "2799900001",
      "media": {
        "fileId": "USE YOUR FILE ID HERE",
        "caption": "First Image File"
      }
    }
  ]
}

Uploaded media file will be available for message sending for 30 days after uploading

The alternative is to do one HTTP request that contains the file data inline:

{
  "messages": [
    {
      "channel": "whatsapp",
      "to": "2799900001",
      "content": "/9j/4AAQSkZJRgABAQEASABIAAD/2w...SDayT2Nha/OIUS3FhlyHzB8ic6ctekf/9k=",
      "media": {
        "contentType": "image/png",
        "caption": "First Image File"
      }
    }
  ]
}

The examples in the Clickatell documentation that I refer to are called:

  • Whatsapp: By reference media: One message
  • Whatsapp: Inline media: One message

If you use the method to send by reference (involving two HTTP requests), it would be beneficial to reuse your curl object ($ch) between all HTTP requests (even for other messages) so that you reuse your HTTP connection for lower latency and reduced CPU usage on your side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文