0

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....

1
  • 5
    Yes. php.net/json_decode (Set the second parameter as TRUE to get an array) Commented Mar 11, 2014 at 13:03

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

3 Comments

thank u fro the fast rply... :)
Glad to help. Feel free to accept the answer if it proved useful :)
need to have 15 reputation for accept it.. :(
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.