Question

When I try to get data from an API using

file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");

I got the result as a string. Is there any way to convert it back to array? The string which I got is

string(202) "{"query":{"count":1,"created":"2014-03-11T13:00:31Z","lang":"en-US","results":{"rate":{"id":"USDINR","Name":"USD to INR","Rate":"60.99","Date":"3/11/2014","Time":"9:00am","Ask":"61.00","Bid":"60.98"}}}}"{"error":"","msg":""}

please help me....

Was it helpful?

Solution

In your request, you ask that the returned format be JSON-encoded (via the format=json parameter), so you can just decode the response into an array using json_decode:

$response = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");
$response = json_decode($response, true);

// Parse the array as you would any other

OTHER TIPS

You have a JSON answer here so jo need to dekode that.

<?php
$s =  file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");$data = 
$data = json_decode($s,true);
?>

and the $data variable will be an array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top