2012-09-13 52 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