1

Sorry this is a total newb question but there is this really great C-library available and I would like to call functions from it using php. However I have a linux server. Here are the specs:

  • Apache version 2.2.17
  • PHP version 5.2.17
  • MySQL version 5.1.56-community-log
  • Architecture x86_64
  • Operating system linux
  • Kernel version 2.6.32-29.1.BHsmp

Would I be able to call the functions from the C-library in php (ex. using php exec()) if the C-library is on the linux server? If so does the library need to be re-compiled using gcc?

Thanks much!

user784637
  • 15,392
  • 32
  • 93
  • 156
  • Probably dupe of [Calling C/C++ library function from PHP](http://stackoverflow.com/questions/2479402/calling-c-c-library-function-from-php) – DaveRandom Aug 22 '11 at 23:28
  • Thanks Dave. I looked at the question and it doesn't specify if the client's machine is running linux or windows server. In my case it's linux so I assume there may be another step as far as recompiling the library to work under linux. – user784637 Aug 22 '11 at 23:30
  • You would need to recompile the library - probably regardless of OS - in order to access it directly from PHP. You need to re-format/rewrite the source code so it can be compiled as a PHP extension. If you have a look at the Zend docs linked on the other question they will tell you how to do this, but you will need to know some C (obviously). – DaveRandom Aug 22 '11 at 23:35

1 Answers1

1

With those specs. you basically have two options.

  1. Create a wrapper library which acts as a PHP extension which enables you to call your C-functions directly from PHP, for example mylib_awesome_func('hello');

  2. Create a command line utility which acts as an interface to your C library and then call this tool with exec() in PHP.

Option one could be considered more "clean" but is definitely harder, whereas option two may be very easy but may in some cases not be possible depending on what kind of data needs to be transmitted/manipulated back and forth to and from the library.

Zeta Two
  • 1,776
  • 1
  • 18
  • 37
  • 1
    Don't forget trust issues: Do you trust the library to run inside your webserver? A separate program can be run with lower privileges, but spawing new processes is more expensive than loading a module. – Kerrek SB Aug 23 '11 at 02:19