當我提交時,此代碼需要5秒鐘才能運行。我應該怎樣做才能獲得更好的表現?首先我使用了file_gets_content,但花費了更多時間。 我分裂的名字,因爲我需要名字和姓氏將其添加到網址。網址=網址+姓氏+名字。獲取JSON的值需要很長時間
<form method='post' action='index.php'>
<input type='submit' name='submit'>
</form>
<?php
/* This is how the URL should look like
// $url = "http://api.suredbits.com/nfl/v0/stats/wagner/bobby";
*/
$url = "http://api.suredbits.com/nfl/v0/stats/";
if(isset($_POST['submit'])) {
$names = array();
array_push($names, 'Bobby Wagner', 'Zach Brown', 'Mason Foster',
'Preston Smith', 'Josh Norman', 'Ryan Kerrigan',
'Earl Thomas', 'Richard Sherman', 'Chandler Jones',
'Marcus Peters');
foreach($names as $n) {
$split_name = explode(" ", $n);
$url .= $split_name[1] . '/' . $split_name[0];
//get json
$data = loadFile($url);
$json_data = json_decode($data, true);
echo $n . ' Tackles: ' . $json_data[0]["defense"]["tackle"];
//reset url
$url = "http://api.suredbits.com/nfl/v0/stats/";
}
}
function loadFile($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
您正在做10個查詢...這是需要時間。嘗試手動執行單個查詢(瀏覽器或終端)以檢查響應時間如此長的原因。 –
單個查詢甚至不需要一秒鐘。但我需要多個查詢,因爲我想呈現多個玩家。也許使用AJAX加載可以幫助? – Err404r