1

我尝试通过 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!!!

有什么想法吗?

4

1 回答 1

2

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

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

不幸的是,这条小线让我难倒了一段时间。

于 2011-04-23T15:55:57.900 回答