-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.
Just for the record, by the code
while ($row[$count] = ibase_fetch_assoc($Ds_Retorno)){
 $count++;
}

$json = json_encode($row);
I believe that it will be an array, soon will have to iterate and then inside the foreach yes will catchforeach ($json as $value) { $value->NUMERO }
and will be an INSERT per loop, for example.– Guilherme Nascimento
Thanks Galera!! Guilherme, a doubt. Inside the foreach I also have to put the INSERT ?
– Robson Freitas
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')");
– Robson Freitas
@Robsonfreitas Give a
var_dump($json);
to check what data the API is receiving, without knowing the structure of JSON, it becomes difficult.– Valdeir Psr