Handling data Websevice json

Asked

Viewed 22 times

0

When running a url returns me the data below. I’m trying to treat it more I don’t know how to do it because the first time I work with json and the variables are empty what I’m doing wrong already Im on other posts more I can’t find the solution

[{"n_recep2":95000,"dt_exame":"2012-08-30","nome_pac":"MARISA OLIVEIRA SILVA","desconv":"IPE","idade":56,"cpf":"75298848087","desconv_1":"IPE","nome_pac_1":"MARISA OLIVEIRA SILVA","titulo":"MAMOGRAFIA / ECOGRAFIA MAMÁRIA:","resultado":"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1046{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset0 Tahoma;}}\r\\viewkind4\\uc1\\pard\\fs22 Estudo conjunto de mamografia e ecografia mam\\'e1ria.\\f1\\par\r\\f0 Pele, tecido subcut\\'e2neo, ar\\'e9ola e regi\\'e3o retroareolar sem altera\\'e7\\'f5es.   \\par\rMamas densas mostrando distribui\\'e7\\'e3o assim\\'e9trica e heterog\\'eanea do tecido fibroglandular.\\par\rCalcifica\\'e7\\'f5es benignas em ambas as mamas. \\par\rA mama esquerda mostra no raio de 12h. uma forma\\'e7\\'e3o c\\'edstica. Mede em torno de 5.5 x 3.9  mm.\\par\rAus\\'eancia de n\\'f3dulos suspeitos ou microcalcifica\\'e7\\'f5es patol\\'f3gicas.        \\par\rLinfonodos de aspecto n\\'e3o suspeito no prolongamento axilar esquerdo.    \\par\r                   \\f1    \\par\r\\f0\\tab Achados benignos.\\par\r\\tab BI-RADS categoria-2.\\f1\\par\r\\f0\\par\r\\par\r\\par\r\\par\r                    Favor trazer os exames anteriores ao realizar mamografia.\\par\r\\par\r\\f1\\par\r\\f0\\par\r}\r."}]


//string json contendo os dados do paciente
 $json_str = 'http://xxx.70.67.242:4040/api/laudos/95000,75298848087';
//faz o parsing na string, gerando um objeto PHP
 $obj = json_decode($json_str);

//imprime o conteúdo do objeto 
echo "n_recep2: $obj->n_recep2<br>"; 
echo "nome_pac: $obj->nome_pac<br>"; 
echo "resultado: $obj->resultado<br>";
  • But you’re parsing a url instead of the json string. Understand this?

  • I have to consume this url that is there, it is third party

  • In this case, you have to make a request. You don’t just make a string with the url and wait for it to do the request. xD. I’ll formulate an answer for you. Know Curl?

1 answer

1


As already mentioned in my comment, you are parsing a string simple that received the URL of the webservice you want to consume:

//string json contendo os dados do paciente
 $json_str = 'http://xxx.70.67.242:4040/api/laudos/95000,75298848087';
//faz o parsing na string, gerando um objeto PHP
 $obj = json_decode($json_str);

It won’t work! At least not that way.

What you need is to make a request that will return this JSON. To do so, we use the cURL:

$curl = curl_init();
curl_setopt_array(
    $curl,
    array(
        CURLOPT_URL => 'http://xxx.70.67.242:4040/api/laudos/95000,75298848087',
        CURLOPT_RETURNTRANSFER => true
    )
);

$json_str = curl_exec($curl);

$obj = json_decode($json_str);
$obj = $obj[0];

//print_r($obj);

echo "n_recep2: $obj->n_recep2<br>";
echo "nome_pac: $obj->nome_pac<br>";
echo "resultado: $obj->resultado<br>";

Of course this is a simple request. Curl goes much further! I recommend reading the Curl documentation in the PHP manual.

Browser other questions tagged

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