-3

Well I will not take credit for these codes as I found it but I would appreciate if someone can help me display the weather with this following coding I will really appreciate it.

$BASE_URL = "http://query.yahooapis.com/v1/public/yql";

    $yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="sc")';
    $yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";

    // Make call with cURL
    $session = curl_init($yql_query_url);
    curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
    $json = curl_exec($session);
    // Convert JSON to PHP object
    $phpObj =  json_decode($json);
    echo '<pre>';print_r($phpObj).'<pre>';

I just want this code to display the weather of a particular place with some variable which I can echo like

echo $city;
echo $temp;

something like this.

really thank you for your valuable time and kindness for helping

noufalcep
  • 3,446
  • 15
  • 33
  • 51
noob
  • 9
  • 3
  • Welcome to stackoveflow.. for starters... Don't forget to include the dollar amount your willing to pay for someone to do this for you.. oh wait.. stackoverflow isnt a coding service -- its a forum where you can ask questions about your code and have others answer them for you.. Not a `I want someone to code this/that for me` forum. For starters, Please read http://stackoverflow.com/help/how-to-ask completely to learn how to politely request help and ask good questions – mike510a Dec 26 '16 at 10:43

2 Answers2

0

For Php Object:

$phpObj =  json_decode($json);    // converting to object
echo $phpObj->property_name;

For Php Array:

$phpArr =  json_decode($json, true);    // converting to array
echo $phpArr['index'];
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
  • im not good at it but i tried to echo echo $phpObj['city']; and then i get error Fatal error: Cannot use object of type stdClass as array in – noob Dec 26 '16 at 08:45
  • Put a print_r() after json_encode() and check the data – Mayank Pandeyz Dec 26 '16 at 08:50
  • $phpArr = json_decode($json, true); // converting to array echo print_r($phpArr['index']); and doing so i get 1 being echoed – noob Dec 26 '16 at 08:51
0

ok i got it and sharing the codes so that it could be of use to someone who is looking for weather api

$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
    $yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="sc")';
    $yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
     //Make call with cURL
    $session = curl_init($yql_query_url);
    curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
    $json = curl_exec($session);
    // Convert JSON to PHP object
    $phpObj =  json_decode($json);
    echo $phpObj->query->results->channel->location->city.' Weather  <br/>';
    echo 'Current: '.$phpObj->query->results->channel->item->condition->text.', ';
    echo sprintf("%0.0f", ($phpObj->query->results->channel->item->condition->temp - 32) * 5 / 9).'°C <br/>';

    echo $phpObj->query->results->channel->item->forecast[0]->day.': ';
    echo $phpObj->query->results->channel->item->forecast[0]->text.', ';
    echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[0]->low - 32) * 5 / 9).'Min°C - </small>';
    echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[0]->high - 32) * 5 / 9).'Max°C </small><br/>';

    echo $phpObj->query->results->channel->item->forecast[1]->day.': ';
    echo $phpObj->query->results->channel->item->forecast[1]->text.', ';
    echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[1]->low - 32) * 5 / 9).'Min°C - </small>';
    echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[1]->high - 32) * 5 / 9).'Max°C </small><br/>';
noob
  • 9
  • 3