在PHP中获取http请求的字节大小

时间:2012-09-13 14:17:51

标签: php api post httpcontext

您好我正在进行商家帐户系统的整合。

我需要将http请求中的totalbytes发送到我网站上的url。我的网站是用PHP编写的。

在C#中,它的工作原理如下:但要提到的要点是我将如何在PH中执行此操作,因为无法在此找到好的文档。提前致谢

 public void ProcessRequest (HttpContext context) 
{
    context.Response.Buffer = true;

    string response = "";
    string sMethod = "POST";

    //sUrl holds the address to the Verify service, this is a service from AllCharge 
    // that verifies the signature on the notification. This makes sure that the notification
    // was sent by AllCharge.

    //Demo server - use this address during the integaration, remark this line when working
    // with the live server
    string sUrl = "http://demo.allcharge.com/Verify/VerifyNotification.aspx";

    //Live server - use this address when working with the live server, remark this line 
    // during the integration
    //string sUrl = "https://incharge.allcharge.com/Verify/VerifyNotification.aspx";

    Int32 nCount = context.Request.TotalBytes;
    string formParameters = System.Text.Encoding.UTF8.GetString(context.Request.BinaryRead(nCount));

}

5 个答案:

答案 0 :(得分:1)

我能想到的最好的是以下php函数。

function findKb($content){
$count=0;
$order = array("\r\n", "\n", "\r", "chr(13)",  "\t", "\0", "\x0B");
$content = str_replace($order, "12", $content);
for ($index = 0; $index < strlen($content); $index ++){
    $byte = ord($content[$index]);
    if ($byte <= 127) { $count++; }
    else if ($byte >= 194 && $byte <= 223) { $count=$count+2; }
    else if ($byte >= 224 && $byte <= 239) { $count=$count+3; }
    else if ($byte >= 240 && $byte <= 244) { $count=$count+4; }
}
return $count;
}

答案 1 :(得分:1)

试试这个:

$rqsize = (int) $_SERVER['CONTENT_LENGTH'];

这样的事情已被问到here

答案 2 :(得分:1)

$size = intval($_SERVER['CONTENT_LENGTH']);

$size应该是请求的长度。

答案 3 :(得分:1)

PHP是解释语言,要做到这一点相当困难,尝试使用memory_get_usage函数,这里是示例http://it.php.net/manual/en/function.memory-get-usage.php

答案 4 :(得分:0)

我发现使用它的最佳方法是使用:

$size = @file_get_contents('php://input');

感谢帮助人员!