Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 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) {
  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. echo '<pre>';
  46. print_r($results);
  47. }
  48.  
  49.  
  50.  
  51. function seedPlayer($allPlayerMetaData, $con) {
  52. $detailUrlArray = array();
  53. $sql = "";
  54. if($allPlayerMetaData && $allPlayerMetaData->elements) {
  55. foreach ($allPlayerMetaData->elements as $key => $value) {
  56. $playerId = $value->id;
  57. $singlePlayerInfo = new stdClass();
  58. $playerCost = $value->now_cost ? ($value->now_cost/10) : 0;
  59. $playingTime = $value->minutes ? $value->minutes : 0;
  60. $playerPoint = $value->total_points ? $value->total_points: 0;
  61. $gameWeek = 1;
  62. $sqlSel = "select * from players where player_id = $playerId and game_week = 1";
  63. $results = $con->query($sqlSel);
  64. if ($results->num_rows > 0) {
  65. $sql .= "UPDATE players set player_cost = '$playerCost', player_point= '$playerPoint', playing_time = '$playingTime' where player_id=$playerId AND game_week = 1;";
  66. }
  67. else {
  68. $sql .= "INSERT INTO players (player_cost,player_point,game_week,player_id, playing_time) VALUES ('$playerCost','$playerPoint', '$gameWeek', '$playerId', '$playingTime');";
  69. }
  70. }
  71. if (mysqli_multi_query($con,$sql))
  72. {
  73. do
  74. {
  75. // Store first result set
  76. if ($result=mysqli_store_result($con)) {
  77. // Fetch one and one row
  78. while ($row=mysqli_fetch_row($result))
  79. {
  80. printf("%s\n",$row[0]);
  81. }
  82. // Free result set
  83. mysqli_free_result($result);
  84. }
  85. }
  86. while (mysqli_next_result($con));
  87. }
  88. mysqli_close($con);
  89. }
  90.  
  91. }
  92.  
  93. $playerUrl = "https://fantasy.premierleague.com/drf/bootstrap";
  94. $allPlayerMetaData = getDataFromExternalApi($playerUrl);
  95. seedPlayer($allPlayerMetaData, $conn);
  96.  
  97. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement