0

I have no expertise in Curl, php or json. I need to get a specific variable from an external page in order to store it later in my mysql database. In this case, I need to get the result value from "transferencia" under the label USD. I tried some examples given from different pages but no success. The page to extract the value is https://s3.amazonaws.com/dolartoday/data.json

I tried this code, but prints an array, I only need one value.

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, "https://s3.amazonaws.com/dolartoday/data.json?dolarp=$transferencia");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch); 
$json = json_decode($res, true);
print_r($json);

I will appreciate it!

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40

1 Answers1

0

You could use file_get_contents() and json_decode(...,true) to retrieve the json data and turn it into an associative array.

Now all you need to do is reference the element you need, e.g. in your case USD/transferencia:

$url = 'https://s3.amazonaws.com/dolartoday/data.json';
$data = json_decode(file_get_contents($url), true);
// show the value
echo $data["USD"]["transferencia"];

You can use var_dump($data) or print_r($data) to have a look at the structure of the array. This will help in finding the reference to items you might need.

jibsteroos
  • 1,366
  • 2
  • 7
  • 13