php - So i have a json file how i can insert it in mysql? -


i'm looking make php script update mysql every hour json file.

the api

http://backpack.tf/api/igetmarketprices/v1/?key=51f7eb704bd7b8231900000c&appid=730&format=json

how can copy things json , put them in mysql?

i mean that

"ak-47 | aquamarine revenge (battle-scarred)": {     "last_updated": 1436569230,     "quantity": 34,     "value": 2268 },  "ak-47 | aquamarine revenge (factory new)": {     "last_updated": 1436569230,     "quantity": 21,     "value": 9386 }, "ak-47 | aquamarine revenge (field-tested)": {     "last_updated": 1436569230,     "quantity": 55,     "value": 4968 }, "ak-47 | aquamarine revenge (minimal wear)": {     "last_updated": 1436569230,     "quantity": 40,     "value": 6018 }, "ak-47 | aquamarine revenge (well-worn)": {     "last_updated": 1436569230,     "quantity": 40,     "value": 3597 }, "ak-47 | black laminate (battle-scarred)": {     "last_updated": 1436569230,     "quantity": 50,     "value": 345 }, "ak-47 | black laminate (factory new)": {     "last_updated": 1436569230,     "quantity": 8,     "value": 8593 }, "ak-47 | black laminate (field-tested)": {     "last_updated": 1436569230,     "quantity": 141,     "value": 308 }, 

i want go in mysql

enter image description here

this i've got far:

<?php  $json = file_get_contents('http://backpack.tf/api/igetmarketprices/v1/?key=51f7eb704bd7b8231900000c&appid=730&format=json'); $obj = json_decode($json,true);  //database connection require_once 'db.php';  /* insert data db */ foreach($obj $item) {    mysql_query("insert `cyberst_csgo`.`items` ( cost, lastupdate)    values ('".$item['value']."'', '".$item['last_updated']."')");  } //database connection close mysql_close($con);  //} ?> 

edit: code generates temporary tab-delimited file , uses load data in file insert file database deletes temporary file.

  1. use mysqli - mysql functions deprecated in php now
  2. edit: cost = value * 0.01.
  3. fastest way load bunch of data mysql load data infile
  4. i haven't tested database import i've used method many times , works quite well.
  5. you may need escape $name $cost $row mysqli_real_escape_string() or $conn->real_escape_string() first. didn't check data.

here's php:

$json = file_get_contents('http://backpack.tf/api/igetmarketprices/v1/?key=51f7eb704bd7b8231900000c&appid=730&format=json'); $obj = json_decode($json,true);  //open temporary file hold data $path  = str_replace('\\','/',realpath(dirname(__file__))); $filename = time().'tmp.csv'; $filename = $path.'/'.$filename; $tmpfile = fopen($filename,'w+');  //data loop through , write temporary file $loopme = $obj['response']['items'];  foreach($loopme $name=>$row) {     //i'm guessing cost     $cost = $row['value'] * 0.01;     //write our tmp file     fwrite($tmpfile,"$name\t$cost\t$row[last_updated]\n"); }  // fastest way load large amounts of data mysql $sql = "load data infile '$filename'   table `cyberst_csgo`.`items`   fields terminated '\t' lines terminated '\n'";  //run mysql query  //delete temporary file unlink($filename); 

Comments