jds43 Posted November 15, 2019 Posted November 15, 2019 Hello, I need to find a way to store a json value returned via REST API and use as a session variable, or something of the like. The user is to input their zip code to provide the closest store location, which is an ID that is returned by a get request (this ID is associated with a page). So this closest store location would now be selected to provide unique information to the user.
Robin S Posted November 16, 2019 Posted November 16, 2019 Have you looked at the $session documentation? If so is there a specific problem you're having? 2
jds43 Posted November 18, 2019 Author Posted November 18, 2019 Thanks @Robin S I've been trying to cobble resources together to make this work. Just not quite sure of how to convert the json data to a session variable yet.
horst Posted November 18, 2019 Posted November 18, 2019 7 minutes ago, jds43 said: Just not quite sure of how to convert the json data to a session variable yet. There are the php functions json_encode and json_decode. You can use one to add it to the session, and the other to read it from the session. (Sorry, on mobile. Therefore no links or examples here. ) 2
jds43 Posted November 18, 2019 Author Posted November 18, 2019 Here's what I have so far, but I'm getting inconsistent results. // Sanitize user input $zipcode = (int) $input->get->zipcode; // Set url for API with sanitized zipcode $link = "https://webapi.test.com/WebAPI/api/v1/test/" . $zipcode; function url_get_contents ($Url) { if (!function_exists('curl_init')){ die('CURL is not installed!'); } $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array('APIKey:************')); curl_setopt($ch, CURLOPT_URL, $Url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); curl_close($ch); return $output; } $jsondata = url_get_contents($link); $location = json_decode($jsondata, true); // Set value of the Nearest Location ID (returned via API based on zipcode input) to session variable named "nearest" $session->nearest = $location['NearestLocationID']; $nearest = $session->nearest; // Let's match the zipcode entered by the user to nearest_location_id field to return the appropriate page $nearest_page = $pages->get('nearest_location_id=[session.nearest]'); echo "<hr>"; if(isset($nearest)) { echo "<h1>$nearest</h1>"; echo "<h2>".$nearest_page->title."</h2>"; } else { echo "<h2>None Selected</h2>"; }
Robin S Posted November 18, 2019 Posted November 18, 2019 7 hours ago, jds43 said: Just not quite sure of how to convert the json data to a session variable yet. JSON is just a string so you can save it to $session as a string: Or you could decode the JSON to an associative array and then save that array to session. The core wireDecodeJson() function is a handy shortcut to force the JSON to decode as associative arrays rather than objects. 1
Robin S Posted November 18, 2019 Posted November 18, 2019 Also, take a look at the WireHttp docs, particularly WireHttp::getJSON() - this method can make your code more streamlined. ? 1
jds43 Posted November 18, 2019 Author Posted November 18, 2019 Thank you @Robin S Allow me to look further at this.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now