Save Json to a mysql database

Asked

Viewed 2,777 times

-1

I don’t know how to process to save one JSON within the database. The whole process is thus:

I am sending from another server through the script below:

 $sql = "SELECT * from mgs_castloang";
 $Ds_Retorno = ibase_query($sql);


$count = 0;
while ($row[$count] = ibase_fetch_assoc($Ds_Retorno)){
    $count++;
}

$json =  json_encode($row);


$ch = curl_init('http://api.dominio.com.br/megs.php');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($json))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

After it runs this process, have the script that is waiting for this JSON:

<?php
include_once "conexao.php";



$n                      = $_POST ["NUMERO"];  
$tipo               = $_POST ["TIPO"]; 
$valor               = $_POST ["VALOR"];  
$status              = $_POST ["STATUS"]; 
$venc                 = $_POST ["VENC"];  

//Inserindo no banco
$sql = mysql_query ("INSERT INTO gr_api (NUMERO, TIPO,VALOR,STATUS, VENC  ) 
VALUES ('$n', '$tipo', '$valor', '$status', '$venc')");

Only nothing saved in the database. The connection works perfectly, but I do not know if the JSON not enough in the API or the way to save the data in the database is incorrect.

1 answer

1


Since you are sending a JSON, it is not possible to capture the values with $_POST. This is only for type requests x-www-form-urlencoded and multipart/form-data.

To capture sending JSON, you can use file_get_contents or fgets, for example:

file_get_contents:

<?php

    include_once "conexao.php";

    $json = json_decode( trim(file_get_contents('php://input')) );
  • The file_get_contents('php://input') will retrieve all input data (sent from the request)

  • trim will delete JSON start and end spaces

fgets:

<?php

    include_once "conexao.php";

    $json = json_decode( trim(fgets(STDIN)) );
  • STDIN is a constant that will indicate the path for PHP to read input data (usually php://stdin).

  • fgets will read the contents of this input value.


To capture JSON values (after one of the above steps), it will depend on the JSON structure. You can do:

$json->NUMERO;
$json->TIPO;

Or else

foreach($json as $key => $value) {
    echo "{$value}", PHP_EOL;
}
  • Just for the record, by the code while ($row[$count] = ibase_fetch_assoc($Ds_Retorno)){&#xA; $count++;&#xA;}&#xA;&#xA;$json = json_encode($row); I believe that it will be an array, soon will have to iterate and then inside the foreach yes will catch foreach ($json as $value) { $value->NUMERO } and will be an INSERT per loop, for example.

  • Thanks Galera!! Guilherme, a doubt. Inside the foreach I also have to put the INSERT ?

  • 1

    I think I’m still doing something wrong. Can you help me ? the data doesn’t go to the bank. I need it to be saved in a loop. Because JSON comes with multiple records. From now on I’m very grateful for the help. <? php include_once "connection.php"; $json = json_decode( Trim(file_get_contents('php://input')) ); foreach ($json as $value) { $value->NUMERO; } $sql = mysql_query ("INSERT INTO gr_api (NUMERO, TYPE,VALUE,STATUS, VENC) VALUES ('$value->NUMERO', '$value->TYPE', '$value->VALUE', '$value->STATUS', '$value->VENC')");

  • @Robsonfreitas Give a var_dump($json); to check what data the API is receiving, without knowing the structure of JSON, it becomes difficult.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.