0

I have been able to parse actual .json files, but this link I can't seem to parse.

http://forecast.weather.gov/MapClick.php?lat=36.321903791028205&lon=-96.80576767853478&FcstType=json

I am thinking because the link itself is not a .json file but a json formatted link... and I am having issues trying to parse it... even if I start by using...

    <?php

    $url = "http://forecast.weather.gov/MapClick.php?lat=36.321903791028205&lon=-96.80576767853478&FcstType=json";
    $json = file_get_contents($url);
    $json_a = json_decode($json,true);


    // <---------- Current Conditions ----------> //
        //Display Location
        $location_full = $json_a['location']['areaDescription'];

?>

And the on my page I want to display this information I have:

<?php    
    require 'req/weatherinfo.php';  
?>
<!DOCTYPE html>
<html>
    <head>
        <title>PawneeTV Weather</title> 
   </head>

    <body>  
    <?php echo $location_full; ?><p>    
    </body>
</html>

Any ideas why its generating a blank page? I have cleared the errors now it just doesn't display anything. I've done with many times with a .json file source, it works with this source http://api.wunderground.com/api/43279e1c0b065c2e/forecast/q/OK/Pawnee.json, but will not work with a link thats ends with =json instead of .json

Duguayster
  • 11
  • 2
  • No, it doesn't matter what the URL looks like. What *do* you get? Have you tried echoing the fetched JSON, and the decoded array? – deceze Apr 01 '16 at 12:39
  • Try to `var_dump` `$json` before decoding and `$json_a` after decoding and see what you get. – Gino Pane Apr 01 '16 at 12:43
  • It goes matter with what the link looks like with the fetch, it works fine when I save that same exact information and save it as a .json file and using the exact same code it will not work with the link ending with =json. – Duguayster Apr 02 '16 at 20:49
  • I have tried the var dump but am really unsure if I am doing it right, would it be possible to provide a code example please? – Duguayster Apr 02 '16 at 20:49

1 Answers1

-1

You can not use file_get_contents in that case. More explanation about this you can read here. This code is working:

<?php

    $url = "http://forecast.weather.gov/MapClick.php?lat=36.321903791028205&lon=-96.80576767853478&FcstType=json";

    // create curl resource
    $ch = curl_init();

    // set url
    curl_setopt($ch, CURLOPT_URL, $url);

    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

    // $output contains the output string
    $output = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch); 

    $json_a = json_decode($output,true);

// <---------- Current Conditions ----------> //
        //Display Location
        $location_full = $json_a['location']['areaDescription'];
Community
  • 1
  • 1
Tomasz
  • 4,847
  • 2
  • 32
  • 41
  • `file_get_contents` *can* produce the exact same HTTP request as curl. *If* you set it up the same, there's no detectable difference between an HTTP request from either function. – deceze Apr 01 '16 at 12:39
  • I have copied this to the exact spot to where it needs to go and still did not generate anything. :/ Looking through it to see why. – Duguayster Apr 02 '16 at 20:52
  • Right now what I am using as a way around it for the time being is using a slave computer that I have automated to bring up the weather JSON page, copy paste it into Notepad++, save it as JSON, and upload it to my own server every 5 minutes.... and it pops the json file at http://duguayster.com/sites/pawneetv/nws_json/req/nws_pawnee.json – Duguayster Apr 02 '16 at 20:58
  • Of course I would love to find a solution to where I can just decode the feed directly from the link itself. – Duguayster Apr 02 '16 at 20:58
  • It's many years later, but NWS requires a USER-AGENT which is why you can't decode from the link directly. You can curl it with a USER-AGENT https://www.weather.gov/documentation/services-web-api "A User Agent is required to identify your application. This string can be anything, and the more unique to your application the less likely it will be affected by a security event. If you include contact information..., we can contact you if your string is associated to a security event. This will be replaced with an API key User-Agent: (myweatherapp.com, contact@myweatherapp.com)" – aiwetir Jan 02 '23 at 19:03