dquh37673 2011-04-22 22:34
浏览 167
已采纳

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?

  • 写回答

1条回答 默认 最新

  • duanduo7400 2011-04-23 15:55
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭
  • ¥15 Matlab读取根元素出错