Welcome to the nuBuilder Forums!

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

Call external REST API

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
shuray
Posts: 30
Joined: Sun Apr 23, 2023 3:36 pm
Has thanked: 10 times
Been thanked: 5 times

Call external REST API

Unread post by shuray »

Hi!
I would like to preform exterlan API call from PHP code and open a result in new browser tab.

My approach:
There is a form with button. In JS I handle OnClick and call nuRunPHP('MyProcedure');

In that procedure I prepare some server-side dara and do:

Code: Select all

// Prepare POST data 
$options = [ 
	'http' => [ 
		'method'  => 'POST', 
		'header'  => 'Content-Type: application/json', 
		'content' => json_encode($data), 
	], 
]; 

// Create stream context 
$context  = stream_context_create($options); 

// Perform POST request 
$response = file_get_contents($url, false, $context); 

// Display the response 
header("Content-Type: application/pdf");
echo $response; 
This works except that the PHP procedure is called twice.
If you delete 2 lines after

Code: Select all

//Display the response
, the procedure is called only once (as expected), but of course it does not show anything

What is the reason of 2 times calling?

Thank you
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Call external REST API

Unread post by kev1n »

Is the procedure also called twice when you habe just one line in it?

Code: Select all

echo "hello"; 
shuray
Posts: 30
Joined: Sun Apr 23, 2023 3:36 pm
Has thanked: 10 times
Been thanked: 5 times

Re: Call external REST API

Unread post by shuray »

Hmm...
No, just echo does not produce two calls.
Moreover,

Code: Select all

header("Content-Type: text/html");
echo "hello";
does 1 call, but

Code: Select all

header("Content-Type: application/pdf");
echo "hello";
makes two calls

It looks like the Acrobat Reader plugin (at least) in Chrome calls the url http://.../nurunphp.php a second time
Post Reply