[英]Speeding up my PHP curl requests, weather function
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中导致严重错误,因此我需要某种方式将这些同步curl请求转换为某种异步形式,谢谢!
ps抱歉,该无效的缩进
注意 :这与加速请求无关。 采用完全不同的方法,但肯定可以解决您的PHP超时错误。
拟议解决方案 :
这是一个更广泛的问题。 需要大量的技术解释和实现假设。 我将尝试在下面列出要点:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.