Texan78 Posted July 26, 2017 Share Posted July 26, 2017 Hello, It's been awhile since I have had to do this and I can't find any of my old code snippets. So having to go from memory. Just trying to start small and add to this. NWS is now has a JSON API where before they had XML/RSS. So I am trying to update or really create new scripts to use this new API before they take down the old method and my site is broke. I have not see this kind of JSON format before so I am a little lost. How would I go about pulling the Event, Sender and Description from the below example? { "@context": [ "https://raw.githubusercontent.com/geojson/geojson-ld/master/contexts/geojson-base.jsonld", { "wx": "https://api.weather.gov/ontology#", "@vocab": "https://api.weather.gov/ontology#" } ], "type": "FeatureCollection", "features": [ { "id": "https://api.weather.gov/alerts/NWS-IDP-PROD-2466813-2306361", "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [ -104.05, 38.88 ], [ -104.06, 38.86 ], [ -104.6, 38.71 ], [ -104.9, 38.87 ], [ -104.83, 38.98 ], [ -104.39, 39.03 ], [ -104.05, 38.88 ] ] ] }, "properties": { "@id": "https://api.weather.gov/alerts/NWS-IDP-PROD-2466813-2306361", "@type": "wx:Alert", "id": "NWS-IDP-PROD-2466813-2306361", "areaDesc": "El Paso", "geocode": { "UGC": [ "COC041" ], "SAME": [ "008041" ] }, "references": [ "https://api.weather.gov/alerts/NWS-IDP-PROD-2466797-2306350" ], "sent": "2017-07-26T21:22:38+00:00", "effective": "2017-07-26T21:22:38+00:00", "onset": "2017-07-26T21:22:00+00:00", "expires": "2017-07-26T22:00:00+00:00", "ends": "2017-07-26T22:00:00+00:00", "status": "Actual", "messageType": "Update", "category": "Met", "severity": "Severe", "certainty": "Observed", "urgency": "Immediate", "event": "Severe Thunderstorm Warning", "sender": "NWS Pueblo CO", "headline": "Severe Thunderstorm Warning issued July 26 at 3:22PM MDT expiring July 26 at 4:00PM MDT by NWS Pueblo CO", "description": "At 322 PM MDT, a severe thunderstorm was located over northern\nColorado Springs, moving east at 10 mph.\n\nHAZARD...60 mph wind gusts and quarter size hail.\n\nSOURCE...Radar indicated.\n\nIMPACT...Hail damage to vehicles is expected. Expect wind damage to\nroofs, siding, and trees.\n\nVery heavy rain is also occurring with this storm.\n\nLocations impacted include...\nColorado Springs, Air Force Academy, Yoder, Ellicott, Schriever AFB,\nPeterson AFB, Falcon, Cimarron Hills and Security-Widefield.", "instruction": "For your protection move to an interior room on the lowest floor of a\nbuilding.", "response": "Shelter", "parameters": { "eventMotionDescription": [ "2017-07-26T21:22:00.000-06:00...storm...278DEG...11KT...38.94,-104.75" ], "hailSize": [ "1.00" ], "windGust": [ 60 ], "NWSheadline": [ "A SEVERE THUNDERSTORM WARNING REMAINS IN EFFECT UNTIL 400 PM MDT FOR CENTRAL EL PASO COUNTY" ], "VTEC": [ "/O.CON.KPUB.SV.W.0172.000000T0000Z-170726T2200Z/" ], "EAS-ORG": [ "WXR" ], "PIL": [ "PUBSVSPUB" ], "BLOCKCHANNEL": [ "CMAS", "EAS", "NWEM" ], "eventEndingTime": [ "2017-07-26T22:00:00Z" ] } } }, I have tried this just trying to pull the sender data but no luck... <?php $headers[] = 'Connection: Keep-Alive'; $headers[] = 'Content-Type: application/json;charset=utf-8'; $headers[] = 'Accept: application/json'; $userAgent = 'php'; $url = 'https://api.weather.gov/alerts/active?region_type=land'; $cURL = curl_init(); curl_setopt($cURL, CURLOPT_URL, $url); curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers); curl_setopt($cURL, CURLOPT_USERAGENT, $userAgent); curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true); curl_setopt($cURL, CURLOPT_HTTPGET, true); $result = curl_exec($cURL); curl_close($cURL); /* Lets get and parse the data and create the variables */ $obj = json_decode($result); $issued_by = $obj->sender; ?> Link to comment Share on other sites More sharing options...
Jacques1 Posted July 26, 2017 Share Posted July 26, 2017 If you look closely at the data in front of you, I'm sure you can at least get a basic understanding of the structure. JSON deals with hierarchical data just like XML, but it's much, much simpler. If you know JavaScript, then you already know JSON; otherwise, there's an official description which takes just a few minutes to read. The indentation alone tells you that “sender” is definitely not a top-level entry. You have to navigate through multiple levels of objects and arrays. See the “features” key? This points to an array of objects. The first object has a properties key, and this key points to another object which contains your values. $data['features'][0]['properties'] Hint: You'll make things much easier if you decode the data as an associative array rather than an object. 1 Link to comment Share on other sites More sharing options...
requinix Posted August 12, 2017 Share Posted August 12, 2017 Pruned. All three of you, consider yourselves warned. Link to comment Share on other sites More sharing options...
Recommended Posts