Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Retrieve data from a web api in json format Topic is solved

Questions related to customising nuBuilder Forte with JavaScript or PHP.
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Retrieve data from a web api in json format

Unread post by yvesf »

Hello,

I would like to retrieve data from a web api rest json. My thinking was that I will create a run button in an edit form. Behind the 'onclick' event, I will request the api and retrieve the value I am interested in and put it in a field in the edit form.
Capture.PNG
The request is http://localhost/api/mesures/1 and the result of this request is formatted as a json file as below.

Code: Select all

[
    {
        "0": "1",
        "id": "1",
        "1": "star",
        "nom": "star",
        "2": "Alain",
        "prenom": "Alain",
        "3": "464",
        "num": "464",
        "4": "afbe vcerd",
        "mesures": "afbe vcerd",
        "5": null,
        "created": null,
        "6": "2022-05-12 08:37:27",
        "modified": "2022-05-12 08:37:27"
    }
]
I am interested in retrieving the value behind "mesures", in this case "afbe vcerd" and put it in a field measures available in my edit browse form. How can I do that ?
Many thx for your help,

Yves
You do not have the required permissions to view the files attached to this post.
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Retrieve data from a web api in json format

Unread post by kev1n »

Hi,

Use json_decode() to decode the json.

E.g. if it's assigned to a variable called $json:

Code: Select all

$decoded = json_decode($json, true);
$mesures = base64_encode($decoded[0]["mesures"]);

$js = " nuSetValue('id_measures', atob('$mesures')); "; // <-- replace id_measures with the id of your text object Measure
nuJavascriptCallback($js);

yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Retrieve data from a web api in json format

Unread post by yvesf »

Thx Kevin. But how do I send the request and retrieve the data ? ie behind onclick event, I have to :
- send the url http://localhost/api/mesures/1. How do I send this url ?
- retrieve the json file and make the treatment like you propose.
many thanks again, certainly obvious but I don't know :-(.
Last edited by yvesf on Thu May 12, 2022 3:27 pm, edited 1 time in total.
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Retrieve data from a web api in json format

Unread post by yvesf »

Ok understood. It's a get request so, I have to perform that :

Code: Select all

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://localhost/api/mesures/1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close ($ch);
// I retrieve the response into $json variable from my get request
// then I treat the this file in order to integrate it into my environment
$decoded = json_decode($json, true);
$mesures = base64_encode($decoded[0]["mesures"]);

$js = " nuSetValue('id_measures', atob('$mesures')); "; // <-- replace id_measures with the id of your text object Measure
nuJavascriptCallback($js);
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Retrieve data from a web api in json format

Unread post by yvesf »

There are 2 ways to create a button with a custom code behind. You can create a run button, or an input button. In this situation, I think that the input button is more appropriate. Do you confirm ? Could you please give us guidelines to know in which case you use input button vs run button.
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Retrieve data from a web api in json format

Unread post by kev1n »

With the run button you can run/open a nuBuilder form. With the input button, you can run custom code to e.g. execute a PHP procedure etc.
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Retrieve data from a web api in json format

Unread post by yvesf »

curl_init() isn't recognized. Do I have to instanciate something ?
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Retrieve data from a web api in json format

Unread post by kev1n »

yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Retrieve data from a web api in json format

Unread post by yvesf »

in phpinfo(), it seems ok...
Capture.PNG
You do not have the required permissions to view the files attached to this post.
Post Reply