问题描述
我有一个 PHP 脚本,它尝试识别中止的连接,所以它知道,当他的响应没有到达客户端并且他需要稍后安排它时。 (使用该部分的通知 api) 我用 XAMPP 在我的本地 PC 上安装了它。为了检查它的功能,我在 PHPStorm 中使用 xdebugger 并在执行期间关闭浏览器中的选项卡,然后在服务器响应之前。但是,在调试器中,connection_aborted 和 connection_status 始终为 0,而且当我运行脚本时,它永远不会执行,应该在 connection_abort 上执行什么。 (它说它已成功发送,即使它不是。) 我听说 Web 服务器可能存在一些缓存问题,因此无法直接识别连接错误。那么,如果这是问题,我该如何解决?或者还有什么我想念的吗?这是我的代码:
public function processMessage(string $text,string $receiver) {
ignore_user_abort(true);
//also tried adding "session_write_close();" here as suggested in one post
sleep(60);
$message = //do some processing stuff
$messageSending = ['message' => $message];
$messageSending = json_encode($messageSending);
echo $messageSending;
ob_flush();
flush();
$aborted = connection_aborted();
$connectionStatus = connection_status();
if($aborted || $connectionStatus!=0 ) {
error_log("[WARNING] Connection aborted");
$sendDate = null;
}else {
$sendDate = new DateTime();
}
$this->core->insertMessageInDB($receiver,$message,$sendDate);
if(!$aborted) {
$this->core->setDeliveredDate($receivedMessage);
}
}
请注意,我尽可能地分解了业务逻辑,因此“处理程序”函数中使用的很多变量在代码示例中没有定义,但它们在实际代码中。然而,这不是问题的重点,但问题是为什么连接从未显示为中止。
我的 Rest-Interface 是一个相当简单的 PHP 文件:
<?PHP
include_once (__DIR__."/../vendor/autoload.PHP");
use package\MessageProcessor;
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type,Access-Control-Allow-Headers,Authorization,X-Requested-With");
if(!isset($_GET['from']) || !isset($_GET['text'])) {
echo "Malformed";
header("HTTP/1.1 400 Bad Request");
exit();
}
$senderNumber = $_GET['from'];
$content = $_GET['text'];
try {
$processor = new MessageProcessor();
$processor->processMessage($content,$senderNumber);
header("HTTP/1.1 200 OK");
} catch (\GuzzleHttp\Exception\RequestException $e) {
$result = $e->getResponse()->getBody();
$result = json_decode($result,true);
header("HTTP/1.1 400 Bad Request");
}
XAMPP-信息:
+ Apache 2.4.43
+ MariaDB 10.4.11
+ PHP 7.4.4 (VC15 X86 64bit thread safe) + PEAR
+ PHPMyAdmin 5.0.2
+ OpenSSL 1.1.0g
+ ADOdb 518a
+ Mercury Mail Transport System v4.63 (not included in the portable version)
+ FileZilla FTP Server 0.9.41 (not included in the portable version)
+ Webalizer 2.23-04 (not included in the portable version)
+ StrawBerry Perl 5.16.3.1 Portable
+ Tomcat 7.0.103
+ XAMPP Control Panel Version 3.2.4.
+ XAMPP mailTodisk 1.0 (write emails via PHP on local disk in <xampp>\mailoutput. Activated in the PHP.ini as mail default.)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)