1

I have a JavaScript file on my server that contains a function. I would like to develop a REST Api to connect to this server, run the JavaScript function and send back the output.

Is it possible to call a JavaScript function from a php file?

I read this but it doesn't answer my question, because my js file is hosted on the same server as the php file.

Is the V8Js extensions what I am looking for?

Edit

The js function looks like this:

function (line, userWeight, weightunit){

    //logic is here

    var computed = {
                userLengthFtin: userLengthFtin,
                userLevel: userLevel,
                proId: line['id'],
                proLengthFeetin: proLengthFeetin,
                proThick: proThickFtin,
                weightunit: weightunit
            };

            return computed;
        }
halfer
  • 19,824
  • 17
  • 99
  • 186
Louis
  • 2,548
  • 10
  • 63
  • 120
  • What's the JS function do? Can you post it? – mpen Oct 19 '17 at 17:55
  • No I am sorry can't post the real function, but I 'll add its form in the question – Louis Oct 19 '17 at 20:02
  • So it just computes something? And you can't run that directly on the client? I'd port it over to PHP if you can, otherwise set up a little node server to handle API requests for this, and have nginx forward your special endpoint to node instead of PHP. Then you don't have to fiddle with running JS inside of PHP and you can add additional endpoints in Node if you need to. – mpen Oct 19 '17 at 20:10
  • @mpenyesterday Yes it just computes something. No I can't run it directly on the client because the function needs to be confidential and stay on the server. Yes maybe I'll port it to php. I am not I understand the node server option you are talking about. Do you think you could point me to a tutorial or some info where I can evaluate if this is a good solution for me? Thanks! – Louis Oct 21 '17 at 07:38
  • Easiest way to handle requests with Node is to use a little library called Express. [Download node](https://nodejs.org/en/) then [install Express](https://expressjs.com/en/starter/installing.html), get [hello world](https://expressjs.com/en/starter/hello-world.html) running, and then pretty much all you have to do is replace `res.send('Hello World!')` with `res.json(yourFunction())` -- that will JSON-encode the result of your function and send it back. – mpen Oct 22 '17 at 17:18
  • @mpem it seems that I can't install node on the server I am using for this. – Louis Oct 25 '17 at 15:23
  • Not sure how you'd install v8js either then. You can set up a 2nd server with Node on it if you want to. A VPS from DigitalOcean is only $5/mo. – mpen Oct 25 '17 at 16:16

1 Answers1

2

Is it possible to call a javascript function from a php file ?

You would need to hand things over to some other software which can execute JS. This might be through shelling out or it might be though a library such as Selenium or the V8js library you found.

Whatever you choose, it would need to be able to handle the particular needs of the JS (e.g. if the JS expects to be embedded in a webpage with access to a DOM and all the APIs provided by a web browser, then you couldn't simply run it with Node.js).

It would probably be simpler to rewrite the function in PHP.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335