fsockopen https 在 fgets 上冻结

发布于 2024-11-02 23:06:04 字数 1034 浏览 0 评论 0原文

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

有什么想法吗?

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\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!!!

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糯酥胸 2024-11-09 23:06:04

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

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

不幸的是,这句话让我难住了一段时间。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文