2012-09-13 53 views
1

您好,我目前正在开发一个商业账户系统的集成。以PHP格式获取http请求的字节大小

我需要将http请求中的总字节数发送到我网站上的网址。我的网站是用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)); 

} 
+1

是'$ _SERVER ['CONTENT_LENGTH']'你在找什么? – bfavaretto

回答

0

我发现获得此为我用最好的方法是使用:

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

感谢您的帮助!

+0

你必须接受你自己的答案,所以有这个问题的其他人可以看到它。 – Leri

+0

您只能在两天后接受您的回答。 –

+0

我不知道。感谢您的信息。 – Leri

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

试试这个:

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

类似的东西已经问here

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

$size应该是请求的长度。

1

PHP是解释语言,这样做的相当困难,尝试使用memory_get_usage功能,这里是样品http://it.php.net/manual/en/function.memory-get-usage.php