I am parsing 2 JSON
files and saving only some necessary data into MySQL database right away. Problem is in execution time of this script, since there is pretty much over 1000 games in the json file.
What the script does is that it parses first json file which is a list of games, where only some of those games are possible to get embed code from.
Steps to get what I want:
- Parse
$game_list_url
- Decode and save
game_id
into$game_id
Now, since I got $game_id
from $game_list_url
file, I can parse another file, which is a specific json
file of the game I want to get info about - info whether it has embed code
which is final target of this entire script. A way of finding out if the game's json
file has embed
v variable is to check if the $webmaster_game == 1
. If it is, thats the game I want!
So moving on from step 2, the next steps would be:
- Parse
JSON
file of a specific game -->$get_game_by_url
- Decode and save
webmaster_game
into$webmaster_game
Check if
$webmaster_game == 1
a) if not,
break
out of inner cycle and go for nextgame_id
b) if yes, decode and store other info into variables and
execute() query
This script takes too long to execute - it runs over max_execution_time
. What could dramatically simplify the complexity of this or how could I change it so it doesn't take so much time?
EDIT
Regarding to suggested answer as duplicate, extending php_execution_time
does not solve my problem, since I have already done it and it does NOT simplify complexity of my script.
I am looking for a way to reduce the execution time to shortest possible. Can saving the json files save some time, are there any other options?
See my code below
<?php
include './db/connect.php';
$game_list_url = 'https://webmasters.miniclip.com/api/games/en.json';
$data = file_get_contents($game_list_url);
$json = json_decode($data,true);
$stmt = $conn->prepare("INSERT INTO game (game_id, name, main_category_id,
width, height, embed,
big_icon_path, small_icon_path
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
foreach ($json as $row){
//decode json vars to php vars
$game_id = $row['game_id'];
//parse game by game_id to check if it is possible to get embed code of the game
$get_game_by_url = 'https://webmasters.miniclip.com/api/games/'.$game_id.'/en.json';
echo $get_game_by_url;
echo "\n";
$data2 = file_get_contents($get_game_by_url);
$json2 = json_decode($data2,true);
foreach ($json2 as $row2){
$webmaster_game = $row2['webmaster_game'];
//if webmaster_game != 1 then skip this game, because we cannot get embed code from miniclip
if($webmaster_game != "1")
break;
$small_icon_path = $row2['small_icon'];
$big_icon_path = $row2['big_icon'];
$name = $row2['name'];
$main_category_id = $row2['main_category_id'];
$width = $row2['width'];
$height = $row2['height'];
$embed = $row2['embed'];
$stmt->bind_param("isiiisss", $game_id, $name, $main_category_id,
$width, $height, $embed,
$big_icon_path, $small_icon_path
);
$stmt->execute();
}
}
//script done, close all connections
$stmt->close();
$conn->close();
?>