'Clickatel WhatsApp Integration Unable to Send Images
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.
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |