【问题标题】:curl php post - Service Unavailable - Maximum number of active clients reachedcurl php post - 服务不可用 - 达到最大活动客户端数
【发布时间】:2016-07-21 09:32:23
【问题描述】:

您好,我正在做curl PHP POST 请求(因为跨域来源),我遇到了问题。发布请求被发送到托管一些我无法控制的自定义 Web 服务器的机器。第一次请求(如 15-20 次都可以),但在此数量的请求之后,我收到带有错误消息的 503 error 响应

服务不可用 - 已达到最大活动客户端数。

我想 curl 在我每次发送请求时都会创建一个新连接。我还假设机器服务器只能打开几个连接。

这是我的 php 代码:

$data = array("getTags" => array("Start_dav","CutON"), "includeTagMetadata" => false);                                                                    
$data_string = json_encode($data);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,"http://domainipaddres/");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, "SID=8c81775da6");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($ch, CURLOPT_FORBID_REUSE, false);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string),
       'Connection: Keep-alive',
       'Keep-alive: 300'
    )                                                                       
);  

//execute post
$result = curl_exec($ch);   
//close connection
curl_close($ch);

我真的需要弄清楚,但是在尝试不同的选项和标题数据 2 天后,我无法让它工作。如何强制 curl 只使用一个连接?

非常感谢

编辑:我今天发现了一个问题。服务器只能有 3 个活动客户端。我发现服务器的第一个响应标头是 SET cookie,我需要在另一个请求中使用该 cookie。我手动设置它并且它工作。有什么办法可以自动做到这一点?

【问题讨论】:

  • 听上去对方的服务器很有限,一秒钟只能处理几个操作。换句话说,问题不在于连接,而在于他们缺乏处理能力。
  • 在我得到 503 响应后,下一个请求在我等待 cca 10 分钟后工作。但首先像 20 个未在行中发送的请求是可以的。这就是它的奇怪之处。我认为如果在请求后立即关闭连接应该没问题。或者,如果 curl 仍然存在,则可能使用相同的连接进行请求。那不应该有问题
  • 我会尝试不使用 curl 选项 CURLOPT_COOKIE、CURLOPT_SSL_VERIFYPEER、CURLOPT_FORBID_REUSE。看看没有它会发生什么......
  • 另外,不要执行 curl_close($ch);只要你要发布另一个 POST...
  • 没有任何帮助。仍然是同样的问题。我在某处读到 Curl 没有响应最后一个 FIN 标志。也许这就是问题所在。无法查看 curl 是否打开新连接

标签: php post curl connection http-status-code-503


【解决方案1】:

我今天解决了这个问题。我检查了响应标头,发现服务器设置了 cookie。我只需要保存 cookie 并将其用于另一个请求及其工作。以下是部分代码:

$data_string = json_encode($data);
$agent= 'Mozilla/5.0 (Windows; U;Windows NT 5.1; ru; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9';
//open connection
$ch = curl_init();
$f = fopen('request.txt', 'w'); //writes headers to this file
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,"domainipaddress");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);
$tmpfname = dirname(__FILE__).'/cookie.txt'; //saves the cookie from server
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch,  CURLOPT_VERBOSE,  1 ); 
curl_setopt( $ch,  CURLOPT_STDERR,  $f );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_FORBID_REUSE, false);                                                                    
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                        
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string),
       'Connection: keep-alive',
       "Keep-Alive: 300"
    )                                                                       
);  

//execute post
$result = curl_exec($ch);
$headers = curl_getinfo($ch);
fclose($f);

还是谢谢你

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-04
    • 2018-11-27
    • 2020-06-07
    • 2019-10-15
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 2020-07-16
    相关资源
    最近更新 更多