Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "root";
- $dbname = "testing";
- $conn = new mysqli($servername, $username, $password, $dbname);
- function getDataFromExternalApi ($url) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $server_output = curl_exec($ch);
- curl_close($ch);
- $data = json_decode($server_output);
- return $data;
- }
- function multiCurlReq($urlArray) {
- $nodes = $urlArray;
- $node_count = count($nodes);
- $curl_arr = array();
- $master = curl_multi_init();
- for($i = 0; $i < $node_count; $i++)
- {
- $url =$nodes[$i];
- $curl_arr[$i] = curl_init($url);
- curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
- curl_multi_add_handle($master, $curl_arr[$i]);
- }
- do {
- curl_multi_exec($master,$running);
- } while($running > 0);
- for($i = 0; $i < $node_count; $i++)
- {
- $results[] = curl_multi_getcontent ($curl_arr[$i]);
- }
- echo '<pre>';
- print_r($results);
- }
- function seedPlayer($allPlayerMetaData, $con) {
- $detailUrlArray = array();
- $sql = "";
- if($allPlayerMetaData && $allPlayerMetaData->elements) {
- foreach ($allPlayerMetaData->elements as $key => $value) {
- $playerId = $value->id;
- $singlePlayerInfo = new stdClass();
- $playerCost = $value->now_cost ? ($value->now_cost/10) : 0;
- $playingTime = $value->minutes ? $value->minutes : 0;
- $playerPoint = $value->total_points ? $value->total_points: 0;
- $gameWeek = 1;
- $sqlSel = "select * from players where player_id = $playerId and game_week = 1";
- $results = $con->query($sqlSel);
- if ($results->num_rows > 0) {
- $sql .= "UPDATE players set player_cost = '$playerCost', player_point= '$playerPoint', playing_time = '$playingTime' where player_id=$playerId AND game_week = 1;";
- }
- else {
- $sql .= "INSERT INTO players (player_cost,player_point,game_week,player_id, playing_time) VALUES ('$playerCost','$playerPoint', '$gameWeek', '$playerId', '$playingTime');";
- }
- }
- if (mysqli_multi_query($con,$sql))
- {
- do
- {
- // Store first result set
- if ($result=mysqli_store_result($con)) {
- // Fetch one and one row
- while ($row=mysqli_fetch_row($result))
- {
- printf("%s\n",$row[0]);
- }
- // Free result set
- mysqli_free_result($result);
- }
- }
- while (mysqli_next_result($con));
- }
- mysqli_close($con);
- }
- }
- $playerUrl = "https://fantasy.premierleague.com/drf/bootstrap";
- $allPlayerMetaData = getDataFromExternalApi($playerUrl);
- seedPlayer($allPlayerMetaData, $conn);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement