我正在嘗試創建一個Web服務,它需要大量數組(double類型)並返回每個數組的平均值。用REST Web服務處理大量數據
要開始我寫了一個PHP腳本中使用HTTP GET
方法來檢索數組,並計算其平均值,以JSON格式的響應返回它:
<?php
header("Content-Type:application/json");
if(!empty($_GET['array'])) {
//calculate mean of each array and send results
$array = $_GET['array'];
$mean = array_sum($array)/count($array);
respond(200,"OK",$mean);
} else {
//invalid request
respond(400,"Invalid Request",NULL);
}
function respond($status,$message,$data) {
header("HTTP/1.1 $status $message");
$response['status']=$status;
$response['message']=$message;
$response['data']=$data;
$json_response=json_encode($response);
echo $json_response;
}
?>
不過,我意識到非常快,與一個大數組(如15000的值)我的查詢字符串用來傳遞值將變得如此之大,以至於不切實際,如果它甚至工作(我已經讀了許多系統的限制約2000字符的長度URL)。所以,我正在尋找一種不同的方式將數據傳遞給我的服務,這種方式不會受到限制。數組將由客戶端程序生成。我正在爲服務器使用最新的XAMPP堆棧。