nPhoenix

PokéAPI

Jan 15th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. <?php
  2.     //Aiden Mercer
  3.     //https://www.youtube.com/user/aiMercer
  4.     //You can run this locally using MAMP/XAMP.
  5.     //You do not need to change anything except where it says: "Make sure to use the same names for columns" and the MYSQL username/password/Database Name/and Table
  6.     //2014
  7.  
  8.     $pokemonName = null;
  9.     $pokemonType = null;
  10.     $pokemonType2 = null;
  11.     $pokemonAbility = null;
  12.     $pokemonAbility22 = null;
  13.     $pokemonAbility33 = null;
  14.     $pokemonAbility44 = null;
  15.     $pokemonImage = null;
  16.     $pokemonWeight = null;
  17.     $pokemonSpeed = null;
  18.     $pokemonSpD = null;
  19.     $pokemonSpA = null;
  20.     $pokemonHP = null;
  21.     $pokemonEvolutions = null;
  22.     $pokemonAttack = null;
  23.     $pokemonDefense = null;
  24.     $pokemonSpeed = null;
  25.     $pokemonHeight = null;
  26.     $pokemonCounter = 1;
  27.    
  28.     while ($pokemonCounter <= 718) {
  29.         $pokeGET2 = ('http://pokeapi.co/api/v1/pokemon/'. $pokemonCounter .'/');
  30.  
  31.         $pokeread = file_get_contents($pokeGET2);
  32.         $pokeConvert = json_decode($pokeread, TRUE);
  33.         $pokemonName = $pokeConvert['name'];
  34.         $pokemonAbility = $pokeConvert['abilities'][0]['name'];
  35.         $pokemonAbility22 = $pokeConvert['abilities'][1]['name'];
  36.         $pokemonAbility33 = $pokeConvert['abilities'][2]['name'];
  37.         $pokemonAbility44 = $pokeConvert['abilities'][3]['name'];
  38.         $pokemonType = $pokeConvert['types'][0]['name'];
  39.         $pokemonType2 = $pokeConvert['types'][1]['name'];
  40.  
  41.         //Replace empty values.
  42.         if ($pokemonAbility == "") {$pokemonAbility="(None)";}
  43.         if ($pokemonAbility22 == "") {$pokemonAbility22="(None)";}
  44.         if ($pokemonAbility33 == "") {$pokemonAbility33="(None)";}
  45.         if ($pokemonAbility44 == "") {$pokemonAbility44="(None)";}
  46.         if ($pokemonType =="") {$pokemonType = "(None)";}
  47.         if ($pokemonType2 =="") {$pokemonType2 = "(None)";}
  48.  
  49.         $pokemonHP = $pokeConvert['hp'];
  50.         $pokemonAttack = $pokeConvert['attack'];
  51.         $pokemonSpA = $pokeConvert['sp_atk'];
  52.         $pokemonDefense =  $pokeConvert['defense'];
  53.         $pokemonSpD =  $pokeConvert['sp_def'];
  54.         $pokemonWeight =  $pokeConvert['weight'];
  55.         $pokemonSpeed = $pokeConvert['speed'];
  56.         $pokemonHeight = $pokeConvert['height'];
  57.  
  58.         //MySQL stuff.
  59.         mysql_connect('localhost', 'mysql_username', 'mysql_password') or die(mysql_error());
  60.         mysql_select_db("your_db_here") or die(mysql_error());
  61.  
  62.         //Make sure to use the same names for columns
  63.         mysql_query("INSERT INTO your_table_here
  64.           (no, name, type, type2, height, weight, ability1, ability2, ability3, ability4, hp, attack, defense, spattack, spdefense, speed) VALUES ('$pokemonCounter','$pokemonName', '$pokemonType', '$pokemonType2', '$pokemonHeight', '$pokemonWeight', '$pokemonAbility', '$pokemonAbility22', '$pokemonAbility33', '$pokemonAbility44' , '$pokemonHP', '$pokemonAttack', '$pokemonDefense', '$pokemonSpA', '$pokemonSpD', '$pokemonSpeed')")
  65.             or die(mysql_error());
  66.        
  67.         //Used in while look and requests.
  68.         $pokemonCounter++;
  69.  
  70.         //Reset variables.
  71.         $pokemonType = null;
  72.         $pokemonAbility = null;
  73.     }
  74.  
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment