简体   繁体   English

PHP fsockopen 失去连接

[英]PHP fsockopen loses connection

I need to retrieve quite a lot of remote data using soap requests (retrieving about 12000 delivery-orders, and make a request for each separate order).我需要使用soap 请求检索大量远程数据(检索大约12000 个交货订单,并为每个单独的订单发出请求)。 On my test server (windows with Xampp) it works fine, and takes about an hour or so.在我的测试服务器(带有 Xampp 的 Windows)上,它运行良好,大约需要一个小时左右。 Putting the scripts on the production server (Linux), the connection gets lost after about 10 minutes (it varies).将脚本放在生产服务器 (Linux) 上,大约 10 分钟后连接丢失(它会有所不同)。

I tried changing the header from "Connection: Close" to "Connection: Keep-Alive", but that makes retrieving each record so slow for some reason - like 3 records instead of 170 records per minutes.我尝试将标题从“连接:关闭”更改为“连接:保持活动”,但是由于某种原因,这使得检索每条记录的速度变得如此缓慢 - 例如 3 条记录而不是每分钟 170 条记录。 Note sure if it is because of the fputs or the fgets-loop.请注意是因为 fputs 还是 fgets-loop。

Can anybody tell me how to force the connection to be alive for a given period?谁能告诉我如何强制连接在给定时间内保持活动状态?

Basically my script is:基本上我的脚本是:

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;

It is resolved now;现在解决了; using stream_socket_client() instead of fsockopen() seems to work now.使用 stream_socket_client() 而不是 fsockopen() 现在似乎可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM