Save Json to a mysql database

-1

I'm not sure how to process a JSON within the database. The whole process looks like this:

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 he executes this process, there is 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 does not arrive in the API or the way to save the data in the database is incorrect.     

asked by anonymous 12.05.2018 / 22:38

1 answer

1

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

To capture the 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 get all incoming data (sent from the request)

  • trim will delete the JSON start and end spaces

fgets:

<?php

    include_once "conexao.php";

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

  • php://stdin will read the contents of this input value.

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

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

Or even

foreach($json as $key => $value) {
    echo "{$value}", PHP_EOL;
}
    
12.05.2018 / 22:46