0
我處理的一個問題,即從URL的答案(在我的情況下,它是JSON文件),我使用的第二次需要花費一些時間,所以PHP返回我最大的執行時間超過了僅在第二次
Fatal error: Maximum execution time of 30 seconds exceeded in [myfile] on line 54
下面是一些代碼:
save_road.php
<?php
include('simple_html_dom.php');
include('controller.php');
$restrictions = new road_restrictions; ?>
Controller.php這樣
個include('functions-map.php');
class road_restrictions {
function __construct() {
$road_id = get_road_id();
show($road_id);
$informacion = $this->get_all_info($road_id); //goes here
foreach ($informacion as $info) {
$this->save($info);
}
//show($informacion);
}
function get_all_info($road_id) {
foreach ($road_id as $id) {
$road_info = get_info($id); //goes here
if ($road_info !== NULL) {
$info[] = $road_info;
}
}
return $info;
}
function save($info) {
save_road($info);
}
}
功能,map.php
function get_info($id) {
$json = file_get_contents('http://restrictions.eismoinfo.lt/'); //stuck here
$array = json_decode($json, true);
//other code
它只是需要太多的時間在這一點上的代碼。第一次當它被稱爲functions-map.php函數get_road_id()
(其中是相同的代碼來獲取JSON)它工作得很好,但當涉及到get_info
函數file_get_contents
它只是等待一些東西,直到時間結束。
你有什麼建議嗎?
您可以使用ini_set('max_execution_time',180);(3分鐘)增加最大執行時間。但試着在網絡選項卡下的chrome控制檯中跟蹤問題,看看你是否具有較高的TTFB或下載時間。如果等待是因爲下載,請啓用gzip壓縮。 –
我第二個Nikolay的評論,如果這個其他服務器是你的,在HTTP上啓用GZIP壓縮可以產生巨大的差異,比如高達10:1的改進。 – TravisO