0
function weatherData($date_array){
$weather_array = [];
$url = "http://api.worldweatheronline.com/premium/v1/past-weather.ashx?key=#######&q=frankston&format=json&date=";
// Get cURL resource
array_pop($date_array);
foreach($date_array as $value){
$url2 = $url . $value;
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url2
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
$data = json_decode($resp);
$runningTotal = 0;
for ($i=0; $i < 7; $i++) {
$hourly_data = $data->data->weather[0]->hourly[$i]->precipMM;
$runningTotal += $hourly_data;
}
if($runningTotal >= 0.7){
array_push($weather_array, 'wet <img class="icon" src="images/icons8-Rain-26.png"/>');
} else{
array_push($weather_array, 'dry <img class="icon" src="images/images.png"/>');
}
}
return $weather_array;
好了,所以我有這個功能,將輸出的天氣數組或不能確定日期是「溼」或「幹」,我的問題是,如果我有,也許120日期,我想這樣做將花費超過30秒的頁面加載時間導致PHP中的嚴重錯誤,所以我需要一些方法將這些同步捲曲請求以某種形式的異步窗體,謝謝!加快我的PHP捲曲的請求,天氣功能
p.s.對不起,這種垃圾縮進
這將是值得嘗試完成同樣的事情,而不使用第4步。您可以直接請求外部網站,甚至不需要使用您的服務器。 –
@DawidZbiński我同意。這可能是值得的。但是,我猜想,對外部網站進行ajax調用會繼承自己的挑戰。在這裏,我只是試圖說明解決當前問題的要點。無論如何,感謝您閱讀答案並提供您的建議。 –
@Bhavik_Shah是的,當然。我明白你的意思。我只是說這是值得嘗試的,因爲它會加快速度並減輕服務器負擔。當然,這並不總是可能的,因爲它也取決於外部。 –