Friday, 24 January 2020

How to insert json data in mysql in php

I use json data from hitbtc.com api . I fetch my trade balance from hitbtc server using api and insert data in my mysql database using curl php.
Create database table :
id = int (3) auto_increment
currencry = varchar(20)
available = varchar(20)
reserved = varchar(20)
date = datetime

You need two file first ' index.php '  and second ' data.json '
Create data.json file and leave blank. In this file json data saved when you run your index.php file.
now create index.php file and copy given code and paste in index.php file.

<?php
$data = file_get_contents('data.json');          // http://yourdomain/data.json
$data = json_decode($data, true);
$need = array(
    'DOGE',
    'BTC',
    'XRP',
    'BSV'
);
foreach ($data as $key => $value) {
    if (in_array($data[$key]['currency'], $need)) {
        echo $data[$key]['currency'] . " = " . $data[$key]['available'] . " = " . $data[$key]['reserved'];
        echo "<br>";
    }
}

?>
<?php
   $ch = curl_init('https://api.hitbtc.com/api/2/trading/balance');
 curl_setopt($ch, CURLOPT_USERPWD, 'API_KEY : SECRET_KEY'); // API AND KEY
 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json'));
 $return = curl_exec($ch);
  curl_close($ch);

if (time()-filemtime($url) > 10) { // file older than 20 minutes

$decode = json_decode($return, true); //getting the file content as array
$fp = fopen('0json.json', 'w');
fwrite($fp, json_encode($decode));
fclose($fp);

echo $json;

} else {
  // file younger than 20 minutes
$json = file_get_contents($url);
// echo the JSON
echo $json;
}
?>

<?php
define('TIMEZONE', 'Asia/kolkata');  // change timezone as per your need
date_default_timezone_set(TIMEZONE);
?>

        <div class="container box">
          <h3 align="center">Import JSON File Data into Mysql Database in PHP</h3><br />
          <?php
          $connect = mysqli_connect("localhost", "database_username", "database_password", "database_name"); //Connect PHP to MySQL Database
          $sql123 = "TRUNCATE TABLE balance  ";

if ($connect->query($sql123) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $connect->error;
}
         $query = '';
          $table_data = '';
          $date = Date("Y-m-d H:i:s");
         foreach ($data as $key => $value) {//Extract the Array Values by using Foreach Loop
              if (in_array($data[$key]['currency'], $need)) {
          $query .= "INSERT INTO balance(currency, available,reserved,date) VALUES ('".$data[$key]['currency']."', '".$data[$key]['available']."','".$data[$key]['reserved']."','$date'); ";  // Make Multiple Insert Query
           $table_data .= '
            <tr>
       <td>'.$data[$key]["currency"].'</td>
       <td>'.$data[$key]["available"].'</td>
       <td>'.$data[$key]["reserved"].'</td>
      </tr>
           '; //Data for display on Web page
          } }
          if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query
    {
     echo '<h3>Imported JSON Data</h3><br />';
     echo '
      <table class="table table-bordered">
        <tr>
         <th width="33%">Currency</th>
         <th width="33%">Available</th>
         <th width="33%">Reserved</th>
        </tr>
     ';
     echo $table_data;
     echo '</table>';
          }
?>

You can also read this page