1
votes

I am trying to run JS code in the backend using PHP but with no success so far.

Problem:
I restricted access to the API of our MVP to the localhost only, and I would like to call the API via Javascript via the backend (since the request would be blocked if called by the frontend). I thought of calling the Javascript script via PHP with the following code:

<?php
echo '<script type="text/JavaScript"> 
        fetch("https://api.example.com:endpoint");
     </script>'
    ;
?>

and added a print in the API to check if the router is being called.

Issue
When loading the page I get no error in console, nor I get a print, so I expect that JS is running on fronend and not backend. Is there a way to make JS code run in backend?

1
is doing a curl request not an option here?Ramon de Vries
Now ask yourself a question, what's the point in having such API at all?Your Common Sense

1 Answers

-1
votes

This is not an answer, just a long comment.

You are correct that the code you posted is running on the front-end, so the request is likely being blocked as you suspect.

There is no way (that I know) to call that backend-end endpoint and cause the JS code to run, but the code you want to run is on the backend... and your PHP code is running on the backend... so your PHP code could duplicate the functionality of that JS code (if you have access and know what the backend JS code is doing).