2

I have a massive library written in C++ that handles a lot of mathematical operations, using C++ makes it fast and in all honesty, I don't want to re-write the whole functionality.

I am thinking about opening this library up to other, web languages (Such as PHP) and will pass the data between the two using JSON.

The issue, I have not had much experience designing/developing an API in C++ and from thinking about the implementation I have come up with this:

The API can be accessed from a HTTP URL: http://localhost/api/ and pass in different commands into it. For example: getDeter .. Which I know how I will do this using PHP.

The problem that I am having difficult understanding is how will visiting http://localhost/api work in C++? Is there another approach to doing this:

  • Compiling the C++ into a output file main.out
  • Have a function in PHP that calls a command ./main.out [command] [arg]
  • Return the response

I did not want to do this approach as I don't think that it is very clean, but I can't seem to think of an alternative to solving this problem.

P.S. I don't want to really open up the core C++ code and make changes. I would like to do the following (In C++)

function calculateDeter(std::vector<double> input)
{
    // call library function 
    // call another library function 

    // return json data

} 
  • Unless you're doing some really spectacular optimizations in C++, I don't see how a server-side scripting language would be much worse in terms of performance for any average task. You might save a couple of milliseconds... I guess it's up to you to decide if that really matters. That said, you may want to look into simple C++ HTTP frameworks like Crow. – Chris Cirefice Oct 19 '15 at 19:42
2

The technique you are describing is (very roughly) similar to CGI, a way for web servers to call external code of any kind. PHP itself is usually run as a CGI application. If you were to implement it this way, a good place to start would be to look at the FCGI documentation. In short, you could use the FCGI library to write an interface for a web server to call your C++ code.

Another approach would be to embed an HTTP server directly into your application. This is a little simpler, since it removes the need for a separate web server, but leaves you to deal with issues like scalability and security that a full-fledged web server will include out the box. If you went down this route you might use a library like libevent.

  • Thank you for the reply. So essentially, the method which I am describing is NOT the method I want to go down. So, therefore, would you recommend me embedding a HTTP server into my application? Can I still access it through a url like: localhost/api/getDeter/ – Phorce Oct 19 '15 at 20:23
  • Yes. For example in libevent (because I'm familiar with it) you would get incoming requests in an event loop, and write code to match whatever request URIs you wanted to the correct responses. – vrostu Oct 19 '15 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.