如何处理PHP处理程序文件中的错误

发布于 2025-01-29 07:34:37 字数 888 浏览 3 评论 0原文

如果js/send_to_telegram.php文件中存在错误,则错误脚本将起作用。怎么做?

jQuery("form").submit(function () {
        var form_data = jQuery(this).serialize();
        jQuery.ajax({
            type: "POST",
            url: "js/send_to_telegram.php",
            data: form_data,
            success: function (result) {
                donemodal.style.display = "block";
            },
            error: function (jqXHR, exception) {
                errormodal.style.display = "block";
            }
        });
    });

在JS/send_to_telegram.php中,以下代码:

$token = "5306003979:AAEPK2NhlxW";
$chat_id = "497358";
$txt = htmlspecialchars($_POST["text"]);

$sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r");

现在,即使您在$ sendtotelegram中输入错误的令牌,它也会返回成功。如果令牌错了,如何获取错误?

If there is an error in the js/send_to_telegram.php file, then the error script will work. How to do it?

jQuery("form").submit(function () {
        var form_data = jQuery(this).serialize();
        jQuery.ajax({
            type: "POST",
            url: "js/send_to_telegram.php",
            data: form_data,
            success: function (result) {
                donemodal.style.display = "block";
            },
            error: function (jqXHR, exception) {
                errormodal.style.display = "block";
            }
        });
    });

in js/send_to_telegram.php the following code:

$token = "5306003979:AAEPK2NhlxW";
$chat_id = "497358";
$txt = htmlspecialchars($_POST["text"]);

$sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r");

Now, even if you enter the wrong token in $sendToTelegram, it returns success. How to get error if token is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2025-02-05 07:34:37

简短答案:它不使用API​​响应状态代码,除非您告诉他这样做。如果我们发送了多个HTTP请求并获得不同的状态代码怎么办?应该使用哪个?

解决方案:这是您需要的:

<?php

function post($url, $fields = []) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);

    if (is_array($fields) && count($fields)) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
    }

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);

    return json_decode($result);
}


$response = post("https://api.telegram.org/bot{$token}/sendMessage", [
    'chat_id' => $chat_id,
    'text' => $txt,
]);

if ($response->ok) {
    echo '{"message": "ok"}';
} else {
    http_response_code(500);
    echo '{"message": "Something went wrong"}';
}

Short answer: It doesn't use API response status code unless you tell him does that. What if we sent multiple HTTP requests and got different status codes? Which of them should be used?

Solution: This is what you need:

<?php

function post($url, $fields = []) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);

    if (is_array($fields) && count($fields)) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
    }

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);

    return json_decode($result);
}


$response = post("https://api.telegram.org/bot{$token}/sendMessage", [
    'chat_id' => $chat_id,
    'text' => $txt,
]);

if ($response->ok) {
    echo '{"message": "ok"}';
} else {
    http_response_code(500);
    echo '{"message": "Something went wrong"}';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文