【问题标题】:PHP fsockopen loses connectionPHP fsockopen 失去连接
【发布时间】:2015-07-29 23:21:45
【问题描述】:

我需要使用soap 请求检索大量远程数据(检索大约12000 个交货订单,并为每个单独的订单发出请求)。在我的测试服务器(带有 Xampp 的 Windows)上,它运行良好,大约需要一个小时左右。将脚本放在生产服务器 (Linux) 上,大约 10 分钟后连接断开(它会有所不同)。

我尝试将标题从“Connection: Close”更改为“Connection: Keep-Alive”,但这使得检索每条记录的速度因某种原因变得如此缓慢——比如每分钟 3 条记录而不是 170 条记录。注意确定是因为 fputs 还是 fgets-loop。

谁能告诉我如何强制连接在给定时间段内保持活动状态?

基本上我的脚本是:

if (!is_resource($fp))
{
    $fp = @fsockopen($host, $port, $errno, $errstr);
    if (!$fp) { echo "soap_parser error message is: $errstr ($errno)<br />\n";exit;}
    stream_set_timeout($fp,3600);//one hour
}

$soap_out  = "POST " . $path . " HTTP/1.1\r\n";
$soap_out .= "Host: " . $hostname . "\r\n";
$soap_out .= "User-Agent: MYSOAPREQUEST \r\n";
$soap_out .= "Content-Type: text/xml;charset=UTF-8;\r\n";
$soap_out .= "Content-Length: ".strlen($xml_request)."\r\n";
$soap_out .= "Connection: Close\r\n"; 
$soap_out .= "Cache-Control: no-cache\r\n";
$soap_out .= "SOAPAction: ".$soap_action."\r\n\r\n"; 
$soap_out .= $xml_request;

fputs($fp, $soap_out); //send request SOAP
$soap_in = "";

while (!feof($fp)) 
{
       $soap_in .= fgets($this->fp, 512);
}

return $soap_in;

【问题讨论】:

标签: php fsockopen


【解决方案1】:

现在解决了;使用 stream_socket_client() 而不是 fsockopen() 现在似乎可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-30
    • 2013-06-10
    • 2010-11-26
    • 1970-01-01
    • 2012-02-03
    • 2012-02-15
    • 2015-09-20
    • 2013-05-07
    相关资源
    最近更新 更多