Jump to content
jds43

GET json data via REST API and store value in session

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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. )

  • Like 2

Share this post


Link to post
Share on other sites

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>";                        
        }

 

Share this post


Link to post
Share on other sites
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:

2019-11-19_114920.png.41de0b9b7e9a4b10cc5bc92599b4694f.png

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.

2019-11-19_115043.png.4f053652fb7f489b0643936f74160bc7.png

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...