如何使用聊天API使用PHP在聊天中发送本地图像



我正在尝试使用chat API、在whatsApp聊天中发送图像

通过引用:https://blog.chat-api.com/manuals_and_tutorials/api_whatsapp_send_file_php/

在这种情况下,代码如下:

$data = json_encode(array(
'chatId'=>$phone.'@c.us',
'body'=>'URL of the image',     
'filename'=>'picture.jpg',
'caption'=>'Hey! There is a file!'
));

我从html表单发送chartId,filename and caption,我的html表单如下:

<?php 
if(isset($_POST['submit'])){
$apiURL = 'api.chat-api.com/instanceXXXXX/';
$token = 'abcdefgABCDEFG';
$phone = $_POST['phone_no'];
$file_name = $_POST['file_name'];
$caption = $_POST['caption'];


$data = json_encode(array(
'chatId'=>$phone.'@c.us',
'body'=>'URL of the image',  //how can i make my local image to this URL 
'filename'=>$file_name,
'caption'=> $caption
));
$url = $apiURL.'sendFile?token='.$token;
$options = stream_context_create(['http' => [
'method'  => 'POST',
'header'  => 'Content-type: application/json',
'content' => $data
]
]);
$response = file_get_contents($url,false,$options);
echo $response; exit;
}
?>
<html>
<body>
<form action = '' method = ''>
<input type = "text" name = "phone_no" id = "phone_no" />
<img src = "#" name = "img" id = "img" /> // here local(from my desktop) image is i am displaying
<input type = "text" name = "file_name" id = "file_name" />
<input type = "text" name = "caption" id = "caption" />
<input type = "submit" name = "submit" value =  "Send" />
</form>
</body>
</html>

这里我的问题是如何将我的本地图像发送到聊天中,我们是否将本地图像转换为URL。我不理解在这件事上帮我。

非常感谢。注意$data中的URL如下:图像的地址(https://test.com/PHP/picture.jpg)

请转换为基数64,

$photoPath = 'folder/path.jpg';
$getExtension = explode(".",$photoPath);
$base64 = 'data:image/'.$getExtension[1].';base64,'.file_get_contents($photoPath);
$data = json_encode(array(
'chatId'=>$phone.'@c.us',
'body'=>  $base64,  //how can i make my local image to this URL 
'filename'=>$file_name,
'caption'=> $caption
));

最新更新