我尝试通过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!!!
有什么想法吗?
答案 0 :(得分:1)
我通过添加标题解决了这个问题:“连接:关闭\ r \ n \ r \ n”。
对Cookie的初始请求会返回302重定向代码,并且除非您传递该标头,否则它将位于打开的连接上。
不幸的是,这条小线让我感到困惑了一段时间。