电报机器人PHP:警告:文件\u获取\u内容无法打开流:400错误请求

电报机器人PHP:警告:文件\u获取\u内容无法打开流:400错误请求,php,telegram-bot,Php,Telegram Bot,我正在编辑电报机器人的php文件。当我在电报上测试时,它完全没有反应。在PHP命令行上,它说: 警告: 文件获取内容(): 无法打开 流:HTTP请求失败!HTTP/1.1 400上G:\xampp\htdocs\xbot\file.php中的错误请求 第39行 第39行: $result = file_get_contents(request_url('sendMessage'), false, $context); 但是,当我更改函数create_响应时,它会起作用: function

我正在编辑电报机器人的php文件。当我在电报上测试时,它完全没有反应。在PHP命令行上,它说:

警告: 文件获取内容(): 无法打开 流:HTTP请求失败!HTTP/1.1 400上G:\xampp\htdocs\xbot\file.php中的错误请求 第39行

第39行:

 $result = file_get_contents(request_url('sendMessage'), false, $context);
但是,当我更改函数create_响应时,它会起作用:

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命令行上显示完整结果:

字符串(157)“值1”
字符串(219)“值2”
字符串(462)“值3”
字符串(555)“值4”
字符串(246)“值5”
{
“ok”:没错,
“结果”:{
“消息id”:197,
“发件人”:{
“id”:107653xxx,
“名字”:“x机器人”,
“用户名”:“x_机器人”
},
“聊天”:{
“id”:2887198,
“名字”:“xxx”,
“用户名”:“xxx”
},
“日期”:1437240345,
“回复邮件”:{
“信息id”:196,
“发件人”:{
“id”:2887xxx,
“名字”:“xxx”,
“用户名”:“xxx”
},
“聊天”:{
“id”:2887xxx,
“名字”:“xxx”,
“用户名”:“xxx”
},
“日期”:1437240342,
“文本”:“mengetahuinya”
},
“文本”:“值5”
}
}
我很困惑,如何解决这个问题?提前谢谢

以下是完整的代码:

<?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\u message()
要求
create\u response()
返回字符串,而不起作用的代码是在有结果时返回数组,在没有结果时返回字符串。最好它总是返回相同类型的数据

要修复它,请更改
create_response()
函数以始终返回数组,并让
process_message()
按需要使用它,即将其转换为字符串

顺便说一下,您的
return
命令必须是函数中执行的最后一个命令。您有
mysqli\u close($conn)在它之后,如果return在它之上,则永远不会执行它

因此,这两个功能变成:

函数创建_响应($text)
{
$conn=mysqli_connect(“localhost”、“root”、“xxx”、“aq”);
$data=array();
$sql=“选择s.text\u sr作为surat,选择s.no\u sr作为nosurat,选择qi.verseid作为ayat,”。
“qi.ayahtext作为来自古兰经的ayattext qi离开加入苏拉s on”。
“qi.suraid=s.no_sr,其中类似“%$text%”的qi.ayahtext限制为3,5”;
$cari=mysqli\u查询($conn,$sql);
$hasil=array();
如果(mysqli_num_行($cari)>0){
//每行的输出数据
while($row=mysqli\u fetch\u数组($cari)){
$hasil[]=“QS.[.”$row[“surat”]。“-“$row[“nosurat”]。”]:”。
$row[“ayat”]。:“$row[“ayattext”]。”;
}
}
mysqli_close($conn);
返回$hasil;
}
函数进程消息($message)
{
$updateid=$message[“update_id”];
$message_data=$message[“message”];
如果(isset($message_data[“text”])){
$chatid=$message_data[“chat”][“id”];
$message_id=$message_data[“message_id”];
$text=$message_data[“text”];
$responseArr=创建_响应($text);
如果(计数($responseArr)>0){
$response=内爆(“.”,$responseArr)。”;
}否则{
$response=“0结果”;
}
发送回复($chatid,$message,$response);
}
返回$updateid;
}