1

I am new using json data. I am trying to call an api using cURL and converting it into array. I just want the temp data using foreach loop. But i am getting Invalid argument supplied for foreach() error. My code

$cSession = curl_init(); 
curl_setopt($cSession,CURLOPT_URL,"http://api.openweathermap.org/data/2.5/forecast/daily?q=dhaka%2Cbangladesh&mode=json&units=metric&cnt=3");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false); 
$result = curl_exec($cSession);

curl_close($cSession);

//echo $result;
//$i = 3 as i am requesting 3 day data
for ($i=0; $i <3 ; $i++) { 
  foreach ($result as $value) {
    echo $value['temp'][0]['day'];
    echo $value['temp'][0]['min'];

    echo $value['temp'][0]['max'];
    echo $value['temp'][0]['night'];
    echo $value['temp'][0]['eve'];
    echo $value['temp'][0]['morn'];
  }
}

4 Answers 4

1

This should work for you:

$json = json_decode($result);
$res = $json->{'list'};
foreach ($res as $value) {
    echo $value->{'temp'}->{'day'} . "<br>";
    echo $value->{'temp'}->{'min'} . "<br>";
    echo $value->{'temp'}->{'max'} . "<br>";
    echo $value->{'temp'}->{'night'} . "<br>";
    echo $value->{'temp'}->{'eve'} . "<br>";
    echo $value->{'temp'}->{'morn'} . "<br>";
}

In your query string you have the value metric&cnt=3 this is what you want for days to be extracted if you get from cnt=10 you will have 10 results. No need for a for loop.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot alex :) Stucked here for almost an hour
1

With the curl_exec you'll get a json as a string, you have to parse it first to use it. Try this: http://php.net/manual/en/function.json-decode.php

And don't forget to set the second parameter to true if you want the result as an array.

2 Comments

adding true as second parameter json data becomes multidimentional array. Already tried with that
Yes and then you could iterate through it if you want.
1

the result is JSON that you should parse with json_decode first

edit with more code:

...
$result = json_decode($result,true);
for ($i=0; $i <3 ; $i++) {
foreach ($result['list'] as $value) {
    echo $value['temp']['day'];
    echo $value['temp']['min'];
...

1 Comment

added some code that fixes the array reference issues as well
0

curl_exec with the option CURLOPT_RETURNTRANSFER set to true will return the result of fetch the url you have set so your variable $result is a STRING.

You hace to convert to an array:

$cSession = curl_init(); 
curl_setopt($cSession,CURLOPT_URL,"http://api.openweathermap.org/data/2.5/forecast/daily?q=dhaka%2Cbangladesh&mode=json&units=metric&cnt=3");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false); 
$result = curl_exec($cSession);

curl_close($cSession);
$result = json_decode($result,true);
//echo $result;
//$i = 3 as i am requesting 3 day data
for ($i=0; $i <3 ; $i++) { 
  foreach ($result as $value) {
    echo $value['temp'][0]['day'];
    echo $value['temp'][0]['min'];

    echo $value['temp'][0]['max'];
    echo $value['temp'][0]['night'];
    echo $value['temp'][0]['eve'];
    echo $value['temp'][0]['morn'];
  }
}

7 Comments

converting json_decode() getting an error illigal string object
Confirm that your result is a json string before more debuging
echoing result gives this output. {"cod":"200","message":0.5425,"city":{"id":"7701342","name":"Dhaka","coord":{"lon":90.3996,"lat":23.7132},"country":"Bangladesh","population":0},"cnt":1,"list":[{"dt":1419310800,"temp":{"day":18.86,"min":13.24,"max":18.86,"night":13.24,"eve":18.86,"morn":18.86},"pressure":1026.27,"humidity":79,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],"speed":1.25,"deg":294,"clouds":0}]}
I've tryed that string in jsonlint.com and it says that it's incorrect. Also, it's diferent to the string of your url
I was trying on jsonlint.com using this url api.openweathermap.org/data/2.5/forecast/… They are saying its a valid json data
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.