当请求失败[在php中]时,如何在电报机器人中检索sendmessage的结果?

当请求失败[在php中]时,如何在电报机器人中检索sendmessage的结果?,php,telegram-bot,Php,Telegram Bot,我通过php向telegram bot发送请求,方式如下: <?php $res=file_get_contents($request); echo($res); 但当$request出现问题时,我希望从$res收到类似的信息: {"ok":false,"error_code":400,"description":"Bad Request: chat not found"} 相反,我收到一条警告,$res为空: Warning: file_get_contents(*******):

我通过php向telegram bot发送请求,方式如下:

<?php
$res=file_get_contents($request);
echo($res);
但当$request出现问题时,我希望从$res收到类似的信息:

{"ok":false,"error_code":400,"description":"Bad Request: chat not found"}
相反,我收到一条警告,$res为空:

Warning: file_get_contents(*******): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/test/public_html/test.php on line 2

如何访问$res格式的电报响应?

如果需要获取错误响应,则不能使用
文件获取内容
,请尝试使用cURL代替它

$url = "https://api.telegram.org/botTOKEN/getChat?chat_id=-1";

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true
]);
$data = curl_exec($curl);
curl_close($curl);

$result = json_decode($data, true);
$url = "https://api.telegram.org/botTOKEN/getChat?chat_id=-1";

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true
]);
$data = curl_exec($curl);
curl_close($curl);

$result = json_decode($data, true);