php - 为什么 Apache 在 200 秒后向 PHP POST 请求返回零内容长度?

标签 php apache

我有 PHP 脚本向远程 API 发送请求。如果响应需要超过 200 秒的时间才能返回,那么我只会在响应中得到一个内容长度为零的值。我想弄清楚为什么会这样。

为了解决这个问题,我将 Apache 和 PHP 的配置文件中的每个可能的变量设置为超过 300 秒来解决这个问题,正如下面第一个答案所建议的那样。我设置为 300 秒的东西:

  • Apache 超时
  • Apache keep_alive 时间
  • PHP 最大响应时间
  • PHP session.cache_expire 时间
  • PHP 最大执行时间

尽管我仍然始终在 200 秒标记附近获得零内容长度响应。但是,如果花费的时间少于 200 秒,则不会出现此问题。

下面我将描述我们的代码是如何设置的。

发生的事情是 crontab 在我们的服务器上运行一个 shell 脚本,它使用/usr/bin/curl 调用本地主机 URI。本地主机 URI 由 Apache 提供,是一个 PHP 文件,它本身包含以下代码,该代码又使用 cURL 调用远程 API。我们 POST 大约 10KB 的 XML,并期望以 block 的形式接收大约 135KB 的返回。

请求代码如下:

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->_xml_url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_str);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);        
        curl_close($ch);

我在我们的 Apache 日志记录中打开调试,下面是我们得到的结果。在此示例中,请求在 19:48:00 发送,响应在 19:51:23 返回,仅在 200 秒后。

* About to connect() to api.asdf.com port 443 (#0)
*   Trying 555.555.555.555... * connected
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSL connection using RC4-SHA
* Server certificate:
*    subject: snip
*    start date: 2014-03-12 10:22:02 GMT
*    expire date: 2015-04-16 12:32:58 GMT
*    subjectAltName: api.asdf.com matched
*    issuer: C=BE; O=GlobalSign nv-sa; CN=GlobalSign Organization Validation CA - G2
*    SSL certificate verify ok.
> POST /xmlservlet HTTP/1.1

Host: api.asdf.com
Accept: */*
Content-Type: text/xml
Content-Length: 10773
Expect: 100-continue

< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Cache-Control: private
< Content-Type: text/xml
< Server: Microsoft-IIS/7.5
< X-AspNet-Version: 4.0.30319
< X-Powered-By: ASP.NET
< Date: Thu, 06 Nov 2014 19:51:23 GMT
< Content-Length: 0
< 

* Connection #0 to host api.asdf.com left intact
* Closing connection #0

我想知道这段代码是否有问题,或者我在服务器设置中可能遗漏了什么,可能导致内容长度在 200 秒后返回零。

最佳答案

听起来很像是远程 API 服务器上的超时 - 尝试手动访问它,例如通过浏览器或 wget。

关于php - 为什么 Apache 在 200 秒后向 PHP POST 请求返回零内容长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26111027/

相关文章:

php - 重置 PDO 中的光标位置

apache - htaccess RewriteRule to https 不能与其他规则结合使用

php - 如何删除重定向中 URL 的特定部分?

php - 给动态创建的元素一个事件

php - 防止 MySQL 和 InnoDB 并发访问和插入

phpmyAdmin 错误我不明白

php - Composer : How to recursively autoload subfolders in/src

java - Apache Tomcat 上的两个 MySQL 数据源配置

php - Windows 10 上的 Memcache PHP 扩展间歇性工作

html - HTML 中的 Perl CGI 脚本