使用PHP

时间:2017-09-27 12:10:00

标签: php networking http-headers http2

我的问题陈述类似于提到的here,除了我需要服务器响应的请求类型是HTTP / 2。 我通过PHP脚本打开一个telnet连接并发送请求:

<?php
    $fp = fsockopen("10.20.0.120", 80, $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        $out = "GET /mng/1000.txt HTTP/2 \r\n";
        $out .= "Host: 10.20.0.120\r\n";
        $out .= "Connection: Close\r\n";    
        fwrite($fp, $out);
        $out = "\r\n";
        $out .= "\r\n";
        fwrite($fp, $out);
        while (!feof($fp)) {
            echo fgets($fp, 128);
        }    
        fclose($fp);
    }
?>

我知道在HTTP / 2的情况下,头框架必须是二进制格式,因此我需要知道我是否可以使用PHP来实现。 我尝试使用PHP pack 方法,但它没有用。当我执行上面的脚本时,这是它给出的输出:

Client(root)#php above_file.php
HTTP/1.1 400 Bad Request
Content-Length: 0

...当请求的类型为HTTP / 1.1(并在另一个使用HTTP / 1.1替换HTTP / 2的设置上执行)时,我收到的输出是预期的:

Client(root)#php above_file.php
HTTP/1.1 200 OK
Date: Wed, 27 Sep 2017 11:31:57 GMT
Server: Apache/2.2.22 (Fedora)
Last-Modified: Wed, 20 Feb 2013 21:27:15 GMT
ETag: "a4f5b-3e8-4d62e9f40f9be"
Accept-Ranges: bytes
Content-Length: 12
Connection: close
Content-Type: text/plain; charset=UTF-8

Hello world!

0 个答案:

没有答案