Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4. $servername = "localhost";
  5. $username = "root";
  6. $password = "root";
  7. $dbname = "testing";
  8. $conn = new mysqli($servername, $username, $password, $dbname);
  9.  
  10. function getDataFromExternalApi ($url) {
  11. $ch = curl_init();
  12. curl_setopt($ch, CURLOPT_URL, $url);
  13. curl_setopt($ch, CURLOPT_POST, 0);
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  15. $server_output = curl_exec($ch);
  16. curl_close($ch);
  17. $data = json_decode($server_output);
  18. return $data;
  19. }
  20.  
  21. function multiCurlReq($urlArray, $game_week) {
  22. $nodes = $urlArray;
  23. $node_count = count($nodes);
  24.  
  25. $curl_arr = array();
  26. $master = curl_multi_init();
  27.  
  28. for($i = 0; $i < $node_count; $i++)
  29. {
  30. $url =$nodes[$i];
  31. $curl_arr[$i] = curl_init($url);
  32. curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
  33. curl_multi_add_handle($master, $curl_arr[$i]);
  34. }
  35.  
  36. do {
  37. curl_multi_exec($master,$running);
  38. } while($running > 0);
  39.  
  40.  
  41. for($i = 0; $i < $node_count; $i++)
  42. {
  43. $results[] = curl_multi_getcontent ($curl_arr[$i]);
  44. }
  45. $finalArray =array();
  46. $index = $game_week - 1;
  47. foreach ($results as $key => $value) {
  48. $a = json_decode($value);
  49. if($a && $a->history && count($a->history) > 0) {
  50. $a = array(
  51. "id" => $a->history[$index]->element,
  52. "points" => $a->history[$index]->total_points,
  53. );
  54. }
  55. else {
  56. $a = array(
  57. "id" => null,
  58. "points" => 0
  59. );
  60. }
  61. array_push($finalArray, $a);
  62. }
  63. return $finalArray;
  64. }
  65.  
  66.  
  67.  
  68. function seedPlayer($allPlayerMetaData, $con) {
  69. $detailUrlArray = array();
  70. $sql = "";
  71. if($allPlayerMetaData && $allPlayerMetaData->elements) {
  72. foreach ($allPlayerMetaData->elements as $key => $value) {
  73. $url = 'https://fantasy.premierleague.com/drf/element-summary/' . $value->id;
  74. array_push($detailUrlArray, $url);
  75. }
  76. $getPlayerDetail = multiCurlReq($detailUrlArray, 1);
  77. foreach ($allPlayerMetaData->elements as $key => $value) {
  78. $playerId = $value->id;
  79. $singlePlayerInfo = new stdClass();
  80. $playerCost = $value->now_cost ? ($value->now_cost/10) : 0;
  81. $playingTime = $value->minutes ? $value->minutes : 0;
  82. $playerPoint = $getPlayerDetail[$key]['points'] ? $getPlayerDetail[$key]['points']: 0;
  83. $gameWeek = 1;
  84. $sqlSel = "select * from players where player_id = $playerId and game_week = 1";
  85. $results = $con->query($sqlSel);
  86. if ($results->num_rows > 0) {
  87. $sql .= "UPDATE players set player_cost = '$playerCost', player_point= '$playerPoint', playing_time = '$playingTime' where player_id=$playerId AND game_week = 1;";
  88. }
  89. else {
  90. $sql .= "INSERT INTO players (player_cost,player_point,game_week,player_id, playing_time) VALUES ('$playerCost','$playerPoint', '$gameWeek', '$playerId', '$playingTime');";
  91. }
  92.  
  93. }
  94. if (mysqli_multi_query($con,$sql))
  95. {
  96. do
  97. {
  98. // Store first result set
  99. if ($result=mysqli_store_result($con)) {
  100. // Fetch one and one row
  101. while ($row=mysqli_fetch_row($result))
  102. {
  103. printf("%s\n",$row[0]);
  104. }
  105. // Free result set
  106. mysqli_free_result($result);
  107. }
  108. }
  109. while (mysqli_next_result($con));
  110. }
  111. mysqli_close($con);
  112. }
  113.  
  114. }
  115.  
  116. $playerUrl = "https://fantasy.premierleague.com/drf/bootstrap";
  117. $allPlayerMetaData = getDataFromExternalApi($playerUrl);
  118. seedPlayer($allPlayerMetaData, $conn);
  119.  
  120. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement