Error saving JSON via fopen, downloaded via Curl

Asked

Viewed 124 times

-2

The command below, accesses an API via Curl, searches for a JSON file, creates a folder inside the /logs folder, and saves the JSON file, so far no problem. However when saving the JSON file it is formatted as follows:

{\"codigo\":\"073279\",\"codigoBarras\":\"7897186004044\",\"nome\":\"ABRACADEIRA\",\"descricao\":\"KIT NYLON 10\/15\/20CMX3MM BR\",\"marca\":\"WESTERN\",\"procedencia\":\"2\",\"peso\":\"0.035\",\"altura\":\"1.5\",\"largura\":\"9\",\"comprimento\":\"21\",\"preco\":\"5.90\",\"cfop\":\"5405\",\"cst\":\"260\",\"icms\":\"0\",\"estoque\":\"G\",\"embalagem\":\"PCT.C\/80\",\"referencia\":\"AB-80\",\"ncm\":\"39269090\",\"lista\":\"I - INFORMATICA\",\"infAdicionais\":\"ABRA\u00c7ADEIRA - KIT NYLON\"}"

Is Inserted " " in the entire code.

When ordering the file manually to the API, CURLOPT_RETURNTRANSFER => false, or giving "echo" in $Sponse, I receive the file normally, as below:

{"codigo":"073279","codigoBarras":"7897186004044","nome":"ABRACADEIRA","descricao":"KIT NYLON 10/15/20CMX3MM BR","marca":"WESTERN","procedencia":"2","peso":"0.035","altura":"1.5","largura":"9","comprimento":"21","preco":"5.90","cfop":"5405","cst":"260","icms":"0","estoque":"G","embalagem":"PCT.C/80","referencia":"AB-80","ncm":"39269090","lista":"I - INFORMATICA","infAdicionais":"ABRAÇADEIRA - KIT NYLON"}

Would someone please help me save the file the right way? remembering that this file will run severside for a cron.

follows below what I’m spinning.

<?php

$nomeDir = date('m-Y');
$caminhoArquivo = "";
$hora = date('H-i-d-m-Y');
$cods = [];
$token = file_get_contents("http://localhost/novoprojeto/token.php");
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://api/api/produto/get-all-tabela?usuario=cl054425",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array("Authorization: Bearer $token"),
));

$response = curl_exec($curl);
$erro = curl_errno($curl);
curl_close($curl);
//ERRO 0 NO CURL É IGUAL A SUCESSO.
//Cria a pasta de Log.
if($erro == 0){
if(!is_dir('logs/'.$nomeDir)) {
    mkdir('logs/'.$nomeDir, '0755');}
}
//cria o arquivo json na pasta criada
$caminhoArquivo = 'logs/'.$nomeDir.'/json-fornecedor-'.$hora.'.json';
$arquivo = fopen($caminhoArquivo, 'w+');
fwrite($arquivo, json_encode($response));
fclose($arquivo);

2 answers

0

Change the treatment to json_decode ne file writing.

$caminhoArquivo = 'logs/'.$nomeDir.'/json-fornecedor-'.$hora.'.json';
$arquivo = fopen($caminhoArquivo, 'w+');
fwrite($arquivo, json_decode($response, true));
fclose($arquivo);

Stores it as an associative array.

-1


I got it Just by taking json_encode, because the file already came "encoded",

//cria o arquivo json na pasta criada
$caminhoArquivo = 'logs/'.$nomeDir.'/json-fornecedor-'.$hora.'.json';
$arquivo = fopen($caminhoArquivo, 'w+');
fwrite($arquivo);
fclose($arquivo);

Browser other questions tagged

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