Get a particular data of a json from php

2

how are they? I ask you a question, I have the following code in php:

function getCotizacionDolar(){



$ch = curl_init();

$headers = [
    'Content-Type: application/json', 
    'Authorization: BEARER eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NjkxOTAxMzUsInR5cGUiOiJleHRlcm5hbCIsInVzZXIiOiJhbGVqYW5kcm8zMDkwMUBnbWFpbC5jb20ifQ.oJisOihr0r0NA0_30hTiGiG1OiLmESAdF44uiDz60CcStW2IKU8jnD78eAdeC7SF60QCOWuFU_Rd9SFgX0hYkw'
];

curl_setopt($ch, CURLOPT_URL, 'http://api.estadisticasbcra.com/tasa_badlar');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$respuesta = curl_exec($ch);
curl_close($ch);


$resultado = json_decode($respuesta, true);   

}

What he basically does is bring me a json with the quote of the dollar of the day, the subject is the following, I only want the last value of that json file, how can I ask for the last data? or how can I go through the 120000 data that it brings me to obtain it? I try to go through it with a for, but it does not leave me because it tells me it is not an array. Would someone tell me how I can do it? then I leave what he returns (only part because it is very long):

  

[{"d": "2003-01-02", "v": 21.75}, {"d": "2003-01-03", "v": 18.375}, {"d": " 2003-01-06 "," v ": 18.1875}, {" d ":" 2003-01-07 "," v ": 14.6875}, {" d ":" 2003-01-08 "," v " : 13.4375}, {"d": "2003-01-09", "v": 10.5625}, {"d": "2003-01-10", "v": 8.9375}, {"d": " 2003-01-13 "," v ": 13.75}, {" d ":" 2003-01-14 "," v ": 11.6875}, {" d ":" 2003-01-15 "," v " : 14.3125}, {"d": "2003-01-16", "v": 13.8125}, {"d": "2003-01-17", "v": 13.5}, {"d": " 2003-01-20 "," v ": 14.3125}, {" d ":" 2003-01-21 "," v ": 13.8125}, {" d ":" 2003-01-22 "," v " : 16.3125}, {"d": "2003-01-23", "v": 12.9375}, {"d": "2003-01-24", "v": 10.5625}, {"d": " 2003-01-27 "," v ": 13.4375}, {" d ":" 2003-01-28 "," v ": 12.1875}, {" d ":" 2003-01-29 "," v " : 15.375}, {"d": "2003-01-30", "v": 12.9375}, {"d": "2003-01-31", "v": 11.4375}, {"d": " 2003-02-03 "," v ": 13.125}, {" d ":" 2003-02-04 "," v ": 15}, {" d ":" 2003-02-05 "," v " : 14.5625}]

    
asked by GALS 23.09.2018 в 01:05
source

2 answers

1

The error is simple, the function curl_exec returns true or false nothing plus. That's where the error is.

What could be done for this case would be to add the option CURLOPT_RETURNTRANSFER to return a String with the result of the call.

After that, you can use json_decode () , and then with end ($ array) get the last element.

Ejm

$ch = curl_init();

$headers = [
    'Content-Type: application/json', 
    'Authorization: BEARER eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NjkxOTAxMzUsInR5cGUiOiJleHRlcm5hbCIsInVzZXIiOiJhbGVqYW5kcm8zMDkwMUBnbWFpbC5jb20ifQ.oJisOihr0r0NA0_30hTiGiG1OiLmESAdF44uiDz60CcStW2IKU8jnD78eAdeC7SF60QCOWuFU_Rd9SFgX0hYkw'
];

curl_setopt($ch, CURLOPT_URL, 'http://api.estadisticasbcra.com/tasa_badlar');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
$respuesta = curl_exec($ch);
curl_close($ch);
$resultado = json_decode($respuesta);
$ultimoelemento =  end($resultado);  
echo $ultimoelemento->d ;
echo $ultimoelemento->v ;
    
answered by 23.09.2018 / 01:36
source
0

Joel !!!!! just modify a little how to call it, because I did not work the last part of that code. Here I leave as I solve it:

 curl_setopt($ch, CURLOPT_URL, 'http://api.estadisticasbcra.com/tasa_badlar');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
$respuesta = curl_exec($ch);
curl_close($ch);

$resultado = json_decode($respuesta, true);
$ultimoelemento =  end($resultado);  

var_dump($ultimoelemento);
$fecha = $ultimoelemento ["d"];
$valorDolar = $ultimoelemento ["v"];
echo $fecha;?><br><?php
echo $valorDolar;
    
answered by 23.09.2018 в 01:47