如何保存从Webhook接收到的图像?(WhatsApp API)



所以,我开始了解更多的WhatsApp API和我的号码收到的所有消息是通过Webhook发送到我的服务器。短信很好,但是媒体信息让我很头疼。

例如,当用户发送图像时,Facebook的Webhook只向我发送mime_typesha256

谁能告诉我该怎么做?

我需要将其转换为base64,然后在我的服务器上写入文件吗?我需要使用特定的功能吗?我是否需要使用PHP以外的其他编程语言?

我完全搞不懂了。

正如@CBroe指出的那样,要做到这一点,方法是使用媒体端点。

假设message是来自Webhook的消息

// Get the image information, including the download link
$image_url_handle = fopen("https://graph.facebook.com/v13.0/" . $message->id);
$image_url = "";
// Read all of the data before parsing
while (!feof($image_url_handle)) {
$image_url .= fread($image_url_handle);
}
// Close the stream
fclose(image_url_handle);
// Get the url from the image information
$image_url = json_decode($image_url)->url;
// Start a request to the download URL.
$image_stream = fopen($image_url);

注意:没有错误处理或框架,尽管这应该在大多数情况下工作

最新更新