php - 刷新页面时,fsockopen 并不总是执行 mysql 查询

标签 php mysql fsockopen

服务器 A 使用 fsockopen、fwrite 和 fgets 从远程服务器 B 收集信息。远程服务器 B 创建 mysql 查询并提供此信息。当服务器 A 首次加载时,所有信息(来自远程服务器 B)都出现在服务器 A 上 - 这意味着连接工作正常。 但是,有时当页面刷新时(在服务器 A 上)没有显示远程服务器 B 的任何信息。这是为什么?

服务器A上的代码很简单:

function test_http_request($path, $host, $test_request, $port=80){
    $test_request_proc= test_encode($test_request);
    //http request  
    $http_request  = "POST $path HTTP/1.0\r\n";
    $http_request .= "Host: $host\r\n";
    $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
    $http_request .= "Content-Length: " . strlen($test_request_proc) . "\r\n";
    $http_request .= "User-Agent: testConnection/PHP\r\n";
    $http_request .= "\r\n";
    $http_request .= $test_request_proc;
    /////output
    $test_resource = '';
    //open socket
    if(false == ($test_connect =@fsockopen('[remote server Bs IP ]', $port, $errno, $errstr, 10))){die('could not open test connection');}
    //send http_request 
    fwrite($test_connect, $http_request);
    //get response 
    while(!feof($test_connect))$test_resource .= fgets($test_connect, 1160);
    //close socket 
    fclose($test_connect);

    $test_resource = explode("\r\n\r\n", $test_resource, 2);
    return $test_resource;
}

根据远程服务器 B 的错误日志,未执行 mysql 查询(为向服务器 A 提供信息而执行的查询)(资源为 bool 值)。有时它们被执行,有时它们没有被执行。问题不在查询语言中,因为在远程服务器 B 上请求和执行查询时它工作得很好。只有当服务器 A 通过 fsockopen() 请求信息时,问题才会出现。 注意:问题完全出在远程服务器B执行的mysql查询上,每次都会出现之前输出的任何文本。 感谢您的宝贵时间

最佳答案

如果你有两个套接字像你一样互相交谈 - 使用“while(true)” - 并且你重新启动其中一个,另一个仍在尝试与第一个实例通信。您需要寻找一些迹象表明(重新启动的)服务器已断开连接并重新打开连接。本质上,对话的一方已经挂断,但另一方仍在倾听……什么也没有。

关于php - 刷新页面时,fsockopen 并不总是执行 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8657999/

相关文章:

php - 删除数组键前缀

php - MySQL w/PHP 中的查询时间结果

MySQL Workbench 6.3 将无法连接到 mysql-5.7.7-rc-osx10.9-x86_64

php - 如何从url检查文件是否存在

PHP Apache fsockopen 到不同的端口

php - 如何在代理后面使用带有 fsockopen 的 imap

php - 用户和公司 - 数据库架构

javascript - 如何在所有文件的头部添加JS,但在magento中的成功页面上添加不同的JS

mysql - 我在 phpmyadmin 中的输入参数的 MYSQL 存储过程中遇到错误?

php - 比较数组键并创建 MySQL 语句的方法