【问题标题】:fsockopen https freezing on fgetsfsockopen https 在 fgets 上冻结
【发布时间】:2011-08-11 06:26:59
【问题描述】:

我尝试通过 PHP fsockopen 连接到服务器以最初获取基本身份验证的 cookie,然后持久连接到响应的 Location 标头中定义的流服务器。

问题是我的代码在 fgets 上冻结并且从未从目标服务器接收到任何响应数据。我通过 Amazon ec2 实例上的端口 443 上的 https 进行连接。服务器通过我服务器终端中的 curl 或通过我的 chome 浏览器连接良好。

  $this->conn = fsockopen('ssl://[destination_server]', 443, $errNo, $errStr, $this->connectTimeout);
  stream_set_blocking($this->conn, 1);

  fwrite($this->conn, "GET " . $urlParts['path'] . " HTTP/1.0\r\n");
  fwrite($this->conn, "Host: " . $urlParts['host'] . "\r\n");
  fwrite($this->conn, "Content-type: application/x-www-form-urlencoded\r\n");
  fwrite($this->conn, "Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");    
  fwrite($this->conn, 'Authorization: Basic ' . $authCredentials . "\r\n");
  fwrite($this->conn, 'User-Agent: ' . self::USER_AGENT . "\r\n");

  list($httpVer, $httpCode, $httpMessage) = preg_split('/\s+/', trim(fgets($this->conn, 1024)), 3);

  //code never gets here!!!

有什么想法吗?

【问题讨论】:

    标签: php https amazon-ec2 fsockopen


    【解决方案1】:

    我通过添加标题解决了这个问题:“连接:关闭\r\n\r\n”。

    对 cookie 的初始请求会返回 302 重定向代码,除非您传递该标头,否则它将位于打开的连接上。

    不幸的是,这条小线让我有一阵子难住了。

    【讨论】:

    • 很高兴你解决了它。感谢您回来分享您的解决方案。
    猜你喜欢
    • 2011-10-11
    • 2012-05-14
    • 2013-01-14
    • 2011-03-05
    • 2010-12-25
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多