fsockopen https冻结fgets

fsockopen https冻结fgets

问题描述:

I attempting to connect to a server via PHP fsockopen to initially get a cookie for basic auth and then to persistently connect to a streaming server defined in the Location header of the response.

The problem is that my code freezes on fgets and never receives any response data from the destination server. I'm connecting via https on port 443 on an Amazon ec2 instance. The server connects fine via curl in my server's terminal or via my chome browser.

  $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
");
  fwrite($this->conn, "Host: " . $urlParts['host'] . "
");
  fwrite($this->conn, "Content-type: application/x-www-form-urlencoded
");
  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 . "
");
  fwrite($this->conn, 'User-Agent: ' . self::USER_AGENT . "
");

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

  //code never gets here!!!

Any thoughts?

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

问题是我的代码在fgets上冻结,从未从目标服务器接收任何响应数据。 我在Amazon ec2实例上通过端口443上的https进行连接。 服务器端口或我的chome浏览器通过卷曲连接服务器。 p>

  $ 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 
 
”); 
 fwrite($ this-> conn,“Host:”。$ urlParts ['host']。“
 
”); 
 fwrite  ($ this-> conn,“Content-type:application / x-www-form-urlencoded 
 
”); 
 fwrite($ this-> conn,“Accept:application / xml,application / xhtml  + xml的,text / html的; q = 0.9,文本/无格式; q = 0.8,图像/ PNG,* / *; q = 0.5" );  
 fwrite($ this-> conn,'Authorization:Basic'。$ authCredentials。“
 
”); 
 fwrite($ this-> conn,'User-Agent:'。self :: USER_AGENT  。“
 
”); 
 
列表($ httpVer,$ httpCode,$ httpMessage)= preg_split('/ \ s + /',trim(fgets($ this-> conn,1024)),3  ); 
 
 //代码永远不会到达!!! 
  code>  pre> 
 
 

有什么想法吗? p> div>

I solved this problem by adding the header: "Connection: Close ".

The initial request for the cookie returns a 302 redirect code and will sit on the open connection unless you pass that header.

Unfortunately, this little line had me stumped for a while.