我想通过SendPhoto
在我的localhost中webhook
。图片不在电报服务器中。所以我需要通过multipart header上传它。
尝试过的代码:
$file=fopen("Untitled.png","rb");
$cont=fread($file,filesize("Untitled.png"));
$headers=array("Content-type: multipart/form-data");
$postfields = array("chat_id" => "108432389", "photo" => "$file");
$ch = curl_init();
$options = array(
CURLOPT_URL => "https://api.telegram.org/bot(Token)/SendPhoto",
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_INFILESIZE => filesize("Untitled.png"),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => $headers
);
curl_setopt_array($ch, $options);
curl_exec($ch);
但它不能发送照片。
我已经在不同的网站上寻找解决方案,但他们的代码与我的代码相同。
为什么这不起作用?
答案 0 :(得分:1)
这是我的PHP函数,它完成了这项工作。
sendPicture($id, "picture.png");
function sendPicture($_chatID, $file_on_server){
$target_url = 'https://api.telegram.org/**<YOUR TOKEN HERE>**/sendphoto';
$file_name_with_full_path = realpath('./'.$file_on_server);
$post = array(
'chat_id' => $_chatID,
'photo' => '@'.$file_name_with_full_path
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
}