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;
        }
7
  • What's the JS function do? Can you post it? Commented Oct 19, 2017 at 17:55
  • No I am sorry can't post the real function, but I 'll add its form in the question Commented Oct 19, 2017 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. Commented Oct 19, 2017 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! Commented Oct 21, 2017 at 7:38
  • Easiest way to handle requests with Node is to use a little library called Express. Download node then install Express, get hello world 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. Commented Oct 22, 2017 at 17:18

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.