Page 1 of 1

Call external REST API

Posted: Sat May 25, 2024 8:31 am
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

Re: Call external REST API

Posted: Sat May 25, 2024 8:57 am
by kev1n
Is the procedure also called twice when you habe just one line in it?

Code: Select all

echo "hello"; 

Re: Call external REST API

Posted: Sat May 25, 2024 5:51 pm
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