PHP用HTTP_RAW_POST_DATA来接收post过来的数据

本文介绍如何使用PHP通过$GLOBALS['HTTP_RAW_POST_DATA']和php://input读取原始POST数据,并提供了一个Python客户端测试示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PHP用HTTP_RAW_POST_DATA来接收post过来的数据

用$GLOBALS[‘HTTP_RAW_POST_DATA’]这个变量就可以接收post raw data了

php配置中,必须启用 always_populate_raw_post_data

always_populate_raw_post_data = On

并且post header中必须 指定 Content-Type 和 Content-Length的值

比如
Content-Type: application/octet-stream
Content-Length: 1097

另外的方法是,使用 php://input , 但 不能处理 enctype=”multipart/form-data”类型的数据

例如

if ( $_SERVER[‘REQUEST_METHOD’] === ‘POST’ )
{
// Read the input from stdin
$postText = trim(file_get_contents(‘php://input’));
}

http://www.codediesel.com/php/reading-raw-post-data-in-php/

自己用Python写个客户端进行测试

import sys
import httplib
import binascii

data = binascii.unhexlify("27130103333536333832")


webservice = httplib.HTTP("10.0.2.2")
webservice.putrequest("POST", "/service.php")
webservice.putheader("Host", "10.0.2.2")
webservice.putheader("Content-type", 'application/octet-stream')
webservice.putheader("Content-length", str(len(data)))
webservice.endheaders()
webservice.send(data)

statuscode, statusmessage, header = webservice.getreply()
print "Response: ", statuscode, statusmessage
print "headers: ", header
print webservice.getfile().read()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值