Telegram Bot PHP:警告:file_get_contents 无法打开流:400 错误请求
Telegram Bot PHP: Warning: file_get_contents failed to open stream: 400 Bad Request
我正在为电报机器人编辑文件 php。当我在电报上测试时,它根本没有任何反应。在 PHP 命令行上,它说:
Warning:
file_get_contents(https://api.telegram.org/bottoken/sendMessage):
failed to open
stream: HTTP request failed! HTTP/1.1 400 Bad Request in G:\xampp\htdocs\xbot\file.php on
line 39
第 39 行:
$result = file_get_contents(request_url('sendMessage'), false, $context);
但是,当我将函数 create_response 更改为:
时它起作用了
function create_response($text)
{
$conn = mysqli_connect("localhost","root","admintma","aq");
$data = array();
$sql = "Select s.text_sr AS surat, s.no_sr AS nosurat, qi.verseid AS ayat, " .
"qi.ayahtext AS ayattext from quranindonesia qi left join surah s on " .
"qi.suraid=s.no_sr where qi.ayahtext like '%$text%' limit 3,5";
$cari = mysqli_query($conn, $sql);
//$hasil = '';
if (mysqli_num_rows($cari) > 0) {
// output data of each row
while($row = mysqli_fetch_array($cari)) {
$hasil = "QS.[".$row["surat"]."-" . $row["nosurat"]. "]:" .
$row["ayat"]. ": " . $row["ayattext"]. ". ";
var_dump($hasil);
}
} else {
$hasil = "0 results";
}
return $hasil;
mysqli_close($conn);
}
但它只显示最后一个结果,而在 php 命令行上显示完整结果:
string(157) "Value1"
string(219) "Value2"
string(462) "Value3"
string(555) "Value4"
string(246) "Value5"
{
"ok": true,
"result": {
"message_id": 197,
"from": {
"id": 107653xxx,
"first_name": "x_bot",
"username": "x_bot"
},
"chat": {
"id": 2887198,
"first_name": "xxx",
"username": "xxx"
},
"date": 1437240345,
"reply_to_message": {
"message_id": 196,
"from": {
"id": 2887xxx,
"first_name": "xxx",
"username": "xxx"
},
"chat": {
"id": 2887xxx,
"first_name": "xxx",
"username": "xxx"
},
"date": 1437240342,
"text": "mengetahuinya"
},
"text": "Value5"
}
}
我很困惑,如何解决这个问题?提前致谢。
完整代码如下:
<?php
include("token.php");
//include("db.php");
function request_url($method)
{
global $TOKEN;
return "https://api.telegram.org/bot" . $TOKEN . "/". $method;
}
function get_updates($offset)
{
$url = request_url("getUpdates")."?offset=".$offset;
$resp = file_get_contents($url);
$result = json_decode($resp, true);
if ($result["ok"]==1)
return $result["result"];
return array();
}
function send_reply($chatid, $msgid, $text)
{
$data = array(
'chat_id' => $chatid,
'text' => $text,
'reply_to_message_id' => $msgid
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents(request_url('sendMessage'), false, $context);
print_r($result);
}
function create_response($text)
{
$conn = mysqli_connect("localhost","root","xxx","aq");
$data = array();
$sql = "Select s.text_sr AS surat, s.no_sr AS nosurat, qi.verseid AS ayat, " .
"qi.ayahtext AS ayattext from quranindonesia qi left join surah s on " .
"qi.suraid=s.no_sr where qi.ayahtext like '%$text%' limit 3,5";
$cari = mysqli_query($conn, $sql);
//$hasil = '';
if (mysqli_num_rows($cari) > 0) {
$hasil = array();
// output data of each row
while($row = mysqli_fetch_array($cari)) {
$hasil[] = "QS.[".$row["surat"]."-" . $row["nosurat"]. "]:" .
$row["ayat"] . ": " . $row["ayattext"] . ". ";
//var_dump($hasil);
}
} else {
$hasil = "0 results";
}
return $hasil;
mysqli_close($conn);
}
function process_message($message)
{
$updateid = $message["update_id"];
$message_data = $message["message"];
if (isset($message_data["text"])) {
$chatid = $message_data["chat"]["id"];
$message_id = $message_data["message_id"];
$text = $message_data["text"];
$response = create_response($text);
send_reply($chatid, $message_id, $response);
}
return $updateid;
}
function process_one()
{
$update_id = 0;
if (file_exists("last_update_id")) {
$update_id = (int)file_get_contents("last_update_id");
}
$updates = get_updates($update_id);
foreach ($updates as $message)
{
$update_id = process_message($message);
}
file_put_contents("last_update_id", $update_id + 1);
}
while (true) {
process_one();
}
?>
问题是你的函数 process_message()
期望 create_response()
到 return 一个字符串,而不起作用的代码是 returning 一个数组是结果,没有结果时是字符串。最好 return 始终使用相同类型的数据。
要修复它,请将 create_response()
函数更改为始终 return 数组,并让 process_message()
根据需要使用它,即将其转换为字符串。
顺便说一句,你的return
命令必须是函数中最后执行的命令。它后面有 mysqli_close($conn);
,如果 return 在它上面,则永远不会执行。
因此,这两个函数变为:
function create_response($text)
{
$conn = mysqli_connect("localhost","root","xxx","aq");
$data = array();
$sql = "Select s.text_sr AS surat, s.no_sr AS nosurat, qi.verseid AS ayat, " .
"qi.ayahtext AS ayattext from quranindonesia qi left join surah s on " .
"qi.suraid=s.no_sr where qi.ayahtext like '%$text%' limit 3,5";
$cari = mysqli_query($conn, $sql);
$hasil = array();
if (mysqli_num_rows($cari) > 0) {
// output data of each row
while($row = mysqli_fetch_array($cari)) {
$hasil[] = "QS.[".$row["surat"]."-" . $row["nosurat"]. "]:" .
$row["ayat"] . ": " . $row["ayattext"] . ". ";
}
}
mysqli_close($conn);
return $hasil;
}
function process_message($message)
{
$updateid = $message["update_id"];
$message_data = $message["message"];
if (isset($message_data["text"])) {
$chatid = $message_data["chat"]["id"];
$message_id = $message_data["message_id"];
$text = $message_data["text"];
$responseArr = create_response($text);
if (count($responseArr) > 0) {
$response = implode(". ", $responseArr) . ".";
} else {
$response = "0 results";
}
send_reply($chatid, $message_id, $response);
}
return $updateid;
}
我正在为电报机器人编辑文件 php。当我在电报上测试时,它根本没有任何反应。在 PHP 命令行上,它说:
Warning: file_get_contents(https://api.telegram.org/bottoken/sendMessage): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in G:\xampp\htdocs\xbot\file.php on line 39
第 39 行:
$result = file_get_contents(request_url('sendMessage'), false, $context);
但是,当我将函数 create_response 更改为:
时它起作用了function create_response($text)
{
$conn = mysqli_connect("localhost","root","admintma","aq");
$data = array();
$sql = "Select s.text_sr AS surat, s.no_sr AS nosurat, qi.verseid AS ayat, " .
"qi.ayahtext AS ayattext from quranindonesia qi left join surah s on " .
"qi.suraid=s.no_sr where qi.ayahtext like '%$text%' limit 3,5";
$cari = mysqli_query($conn, $sql);
//$hasil = '';
if (mysqli_num_rows($cari) > 0) {
// output data of each row
while($row = mysqli_fetch_array($cari)) {
$hasil = "QS.[".$row["surat"]."-" . $row["nosurat"]. "]:" .
$row["ayat"]. ": " . $row["ayattext"]. ". ";
var_dump($hasil);
}
} else {
$hasil = "0 results";
}
return $hasil;
mysqli_close($conn);
}
但它只显示最后一个结果,而在 php 命令行上显示完整结果:
string(157) "Value1"
string(219) "Value2"
string(462) "Value3"
string(555) "Value4"
string(246) "Value5"
{
"ok": true,
"result": {
"message_id": 197,
"from": {
"id": 107653xxx,
"first_name": "x_bot",
"username": "x_bot"
},
"chat": {
"id": 2887198,
"first_name": "xxx",
"username": "xxx"
},
"date": 1437240345,
"reply_to_message": {
"message_id": 196,
"from": {
"id": 2887xxx,
"first_name": "xxx",
"username": "xxx"
},
"chat": {
"id": 2887xxx,
"first_name": "xxx",
"username": "xxx"
},
"date": 1437240342,
"text": "mengetahuinya"
},
"text": "Value5"
}
}
我很困惑,如何解决这个问题?提前致谢。
完整代码如下:
<?php
include("token.php");
//include("db.php");
function request_url($method)
{
global $TOKEN;
return "https://api.telegram.org/bot" . $TOKEN . "/". $method;
}
function get_updates($offset)
{
$url = request_url("getUpdates")."?offset=".$offset;
$resp = file_get_contents($url);
$result = json_decode($resp, true);
if ($result["ok"]==1)
return $result["result"];
return array();
}
function send_reply($chatid, $msgid, $text)
{
$data = array(
'chat_id' => $chatid,
'text' => $text,
'reply_to_message_id' => $msgid
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents(request_url('sendMessage'), false, $context);
print_r($result);
}
function create_response($text)
{
$conn = mysqli_connect("localhost","root","xxx","aq");
$data = array();
$sql = "Select s.text_sr AS surat, s.no_sr AS nosurat, qi.verseid AS ayat, " .
"qi.ayahtext AS ayattext from quranindonesia qi left join surah s on " .
"qi.suraid=s.no_sr where qi.ayahtext like '%$text%' limit 3,5";
$cari = mysqli_query($conn, $sql);
//$hasil = '';
if (mysqli_num_rows($cari) > 0) {
$hasil = array();
// output data of each row
while($row = mysqli_fetch_array($cari)) {
$hasil[] = "QS.[".$row["surat"]."-" . $row["nosurat"]. "]:" .
$row["ayat"] . ": " . $row["ayattext"] . ". ";
//var_dump($hasil);
}
} else {
$hasil = "0 results";
}
return $hasil;
mysqli_close($conn);
}
function process_message($message)
{
$updateid = $message["update_id"];
$message_data = $message["message"];
if (isset($message_data["text"])) {
$chatid = $message_data["chat"]["id"];
$message_id = $message_data["message_id"];
$text = $message_data["text"];
$response = create_response($text);
send_reply($chatid, $message_id, $response);
}
return $updateid;
}
function process_one()
{
$update_id = 0;
if (file_exists("last_update_id")) {
$update_id = (int)file_get_contents("last_update_id");
}
$updates = get_updates($update_id);
foreach ($updates as $message)
{
$update_id = process_message($message);
}
file_put_contents("last_update_id", $update_id + 1);
}
while (true) {
process_one();
}
?>
问题是你的函数 process_message()
期望 create_response()
到 return 一个字符串,而不起作用的代码是 returning 一个数组是结果,没有结果时是字符串。最好 return 始终使用相同类型的数据。
要修复它,请将 create_response()
函数更改为始终 return 数组,并让 process_message()
根据需要使用它,即将其转换为字符串。
顺便说一句,你的return
命令必须是函数中最后执行的命令。它后面有 mysqli_close($conn);
,如果 return 在它上面,则永远不会执行。
因此,这两个函数变为:
function create_response($text)
{
$conn = mysqli_connect("localhost","root","xxx","aq");
$data = array();
$sql = "Select s.text_sr AS surat, s.no_sr AS nosurat, qi.verseid AS ayat, " .
"qi.ayahtext AS ayattext from quranindonesia qi left join surah s on " .
"qi.suraid=s.no_sr where qi.ayahtext like '%$text%' limit 3,5";
$cari = mysqli_query($conn, $sql);
$hasil = array();
if (mysqli_num_rows($cari) > 0) {
// output data of each row
while($row = mysqli_fetch_array($cari)) {
$hasil[] = "QS.[".$row["surat"]."-" . $row["nosurat"]. "]:" .
$row["ayat"] . ": " . $row["ayattext"] . ". ";
}
}
mysqli_close($conn);
return $hasil;
}
function process_message($message)
{
$updateid = $message["update_id"];
$message_data = $message["message"];
if (isset($message_data["text"])) {
$chatid = $message_data["chat"]["id"];
$message_id = $message_data["message_id"];
$text = $message_data["text"];
$responseArr = create_response($text);
if (count($responseArr) > 0) {
$response = implode(". ", $responseArr) . ".";
} else {
$response = "0 results";
}
send_reply($chatid, $message_id, $response);
}
return $updateid;
}