Page 1 of 2

Retrieve data from a web api in json format

Posted: Thu May 12, 2022 12:36 pm
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

Re: Retrieve data from a web api in json format

Posted: Thu May 12, 2022 1:51 pm
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);


Re: Retrieve data from a web api in json format

Posted: Thu May 12, 2022 3:03 pm
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 :-(.

Re: Retrieve data from a web api in json format

Posted: Thu May 12, 2022 3:13 pm
by kev1n

Re: Retrieve data from a web api in json format

Posted: Thu May 12, 2022 3:35 pm
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);

Re: Retrieve data from a web api in json format

Posted: Thu May 12, 2022 3:51 pm
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.

Re: Retrieve data from a web api in json format

Posted: Thu May 12, 2022 3:57 pm
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.

Re: Retrieve data from a web api in json format

Posted: Thu May 12, 2022 4:02 pm
by yvesf
curl_init() isn't recognized. Do I have to instanciate something ?

Re: Retrieve data from a web api in json format

Posted: Thu May 12, 2022 4:05 pm
by kev1n

Re: Retrieve data from a web api in json format

Posted: Thu May 12, 2022 4:42 pm
by yvesf
in phpinfo(), it seems ok...
Capture.PNG