如何HTTP头/身体从PHP套接字要求隔离 [英] How to isolate the HTTP headers/body from a PHP Sockets request

查看:137
本文介绍了如何HTTP头/身体从PHP套接字要求隔离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP中的套接字连接数据发布到Apache网络服务器。我有点新的这种技术,我不知道如何将头从响应的身体分离。

I'm using a socket connection in PHP to post data to an Apache webserver. I'm a bit new to this technique, and I am not sure how to isolate the headers from the body of the response.

发送code:

<?php
// collect data to post
$postdata = array(
    'hello' => 'world'
);
$postdata = http_build_query($postdata);
// open socket, send request
$fp = fsockopen('127.0.0.1', 80);
fwrite($fp, "POST /server.php HTTP/1.1\r\n");
fwrite($fp, "Host: fm1\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($postdata)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");
fwrite($fp, $postdata);
// go through result
$result = "";
while(!feof($fp)){
    $result .= fgets($fp);
}
// close
fclose($fp);
// display result
echo $result;
?>

服务器code:

Server Code:

Hello this is server. You posted:
<pre>
<?php print_r($_POST); ?>
</pre>

在张贴到一台服务器,我得到:

When posting to one server, I get:

HTTP/1.1 200 OK
Date: Fri, 06 Jan 2012 09:55:27 GMT
Server: Apache/2.2.15 (Win32) mod_ssl/2.2.15 OpenSSL/0.9.8m PHP/5.3.2
X-Powered-By: PHP/5.3.2
Content-Length: 79
Connection: close
Content-Type: text/html

Hello this is server. You posted:
<pre>
Array
(
    [hello] => world
)
</pre>

如所预期的。我想,虽然去掉了标题,并从刚读身体的你好,这是服务器.....开始。
我怎样才能可靠地检测头的结束和阅读身体成变量?

As expected. I want to strip off the headers though, and just read the body from "Hello this is server....." onwards. How can i reliably detect the end of the headers and read the body into a variable?

另外,我的答复测试与此另一台服务器:

Also, another server I've tested on replies with this:

HTTP/1.1 200 OK
Date: Fri, 06 Jan 2012 10:02:04 GMT
Server: Apache/2
X-Powered-By: PHP/5.2.17
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

4d
Hello this is server. You posted:
<pre>
Array
(
    [hello] => world
)
</pre>
0

什么是4D和0围绕主体文本??

What are the "4d" and "0" around the body text??

谢谢!

PS之前有人说用袅袅,我不能不幸的是: - (

PS before someone says use CURL, I can't unfortunately :-(

推荐答案

您可以通过双断行分手从身体分离头。它应该是&LT;&CRLF GT;&LT;&CRLF GT; 所以这将的正常的工作:

You can separate the header from the the body by splitting up on a double linebreak. It should be <CRLF><CRLF> so this would normally work:

list($header, $body) = explode("\r\n\r\n", $response, 2);

更可靠,你应该使用正则表达式赶上换行符变化(超级不可能不会发生):

More reliably you should use a regex to catch linebreak variations (super unlikely to ever happen):

list($header, $body) = preg_split("/\R\R/", $response, 2);

4D 0 的东西就是所谓的的chunked~~V编码。 (与另一断行分离它的十六进制数字,inidicate以下原始内容块的长度)。

The thing with the 4d and 0 is called the chunked encoding. (It's hex-numbers separated with another linebreak, and inidicate the length of the following raw content block).

要清除了你就必须在头先看看,看看是否有一个根据传输编码:项。这是它变得复杂,并建议使用现有的HTTP用户态加工类的无数之一。 PEAR 有一个

To clear that up you have to look at the headers first, and see if there is an according Transfer-Encoding: entry. This is were it gets complicated, and advisable to use one of the myriad of existing HTTP userland processing classes. PEAR has one.

这篇关于如何HTTP头/身体从PHP套接字要求隔离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆