我已经创建了一个简单的项目来测试在php 7中将照片发送到电报机器人,但是在启动机器人之后没有任何发送,但是这个项目在php 5.3中运行! 我应该使用它在php 7中有什么不同吗?
$message = file_get_contents("php://input");
$result = json_decode($message,true);
$token = "My_Robot_token";
if($result['message']['text']=='/start')
{
$target_url = "https://api.telegram.org/bot".$token."/sendPhoto";
$file_path = "./img/img1.jpg";
$file_name_with_full_path = realpath($file_path);
$chat_id = $result['message']['chat']['id'];
$photo_send = array(
'chat_id' => $chat_id,
'photo' => '@'.$file_name_with_full_path,
'caption' => 'photo sent!'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $photo_send);
$result=curl_exec ($ch);
file_put_contents('res.txt', $ch);
}
答案 0 :(得分:1)
define('API_KEY','API Token');
function bot($method,$datas=[]){
$url = "https://api.telegram.org/bot".API_KEY."/".$method;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$datas);
$res = curl_exec($ch);
if(curl_error($ch)){
var_dump(curl_error($ch));
}else{
return json_decode($res);
}
}
function sendphoto($ChatId, $photo, $caption){
bot('sendphoto',[
'chat_id'=>$ChatId,
'photo'=>$photo,
'caption'=>$caption
]);
}
$update = json_decode(file_get_contents('php://input'));
var_dump($update);
$message=$update->message;
$chat_id=$message->chat->id;
$text =$message->text;
if($text=='/start'){
sendphoto($chat_id,"Your file_id","photo sent!");//send photo by file_id
sendphoto($chat_id,new CURLFILE("file path"),"photo sent!");//send photo by url
}
答案 1 :(得分:-1)