8

I'm starting with PHP for dynamic web pages. I have some libraries written in ANSI C for getting/setting parameters and other proprietary stuff. I wonder, is there a simple solution to use a wrapper inside PHP to call this functions? Is there a already existing class/library? What would be the best practice to do this on my own? I don't want do make calls to external applications and use stdin/stdout!

Is there a simple example available? I don't want to dig through the Zend documentation for now, I only need a feeling for the complexity.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Andi
  • 888
  • 2
  • 10
  • 24
  • Possible duplicate of [Calling C/C++ library function from PHP](http://stackoverflow.com/questions/2479402/calling-c-c-library-function-from-php) – Harry Johnston Apr 25 '17 at 21:41

4 Answers4

8

Can you package your libraries into a DLL? If so, you can call them through PHP's COM api.

PHP COM Docs: http://us3.php.net/manual/en/book.com.php

Example Code:

<?php  
$com = new COM("DynamicWrapper");
$com->Register("KERNEL32", "Beep", "i=ll", "f=s", "r=l");
$com->Beep(800, 10);

Otherwise you can write a extension that contains a custom wrapper function (ie, execute_through_wrapper('yourfunc')). Here is a doc on writing php functions in C.

http://php.net/manual/en/internals2.funcs.php

Edit:
http://abhinavsingh.com/blog/2008/12/php-extensions-how-and-why/

Here is a quick tutorial on writing extensions in C. It shouldn't be too difficult to write a wrapper function. Once you created the extension, it can be loaded dynamically through dl() (very dangerous, and depreciated).

http://us2.php.net/manual/en/function.dl.php

Those are the only options in your case. There isn't a linux equivalent (.so loader) of the dll loader (its a win32-related api call).

John Himmelman
  • 21,504
  • 22
  • 65
  • 80
  • I forgot to mention, our operating system is Linux for this project. But it should be possible to build a shared library for this libs. – Andi May 18 '10 at 18:54
  • Thanks John, it took a while to read this articles, and I guess writing an extension is not too complicated! – Andi May 19 '10 at 11:40
  • you can very easily write an extension for your library in C, should be a 15 min work if only you need to do is get some data by calling methods of your c library..... – Abhinav Singh Apr 12 '11 at 09:37
2

You could also have gearman act as an intermediary.

Gearman provides a generic application framework to farm out work to other machines or processes that are better suited to do the work. It allows you to do work in parallel, to load balance processing, and to call functions between languages. It can be used in a variety of applications, from high-availability web sites to the transport of database replication events. In other words, it is the nervous system for how distributed processing communicates.

Corey Ballou
  • 42,389
  • 8
  • 62
  • 75
  • Wow - this is comletely new to me! It looks like a lightweight RPC client/server framework, isn't it? Maybe a little bit of overkill for a small application, but for a complex system it might be very useful. An I can use it for communication between multiple processes, written in PHP, C, C++, etc? – Andi May 18 '10 at 19:16
1

What'a about SWIG? http://www.swig.org/

Andi
  • 888
  • 2
  • 10
  • 24
0

At http://pear.php.net/ I found an extension named "inline_c". Unfortunately I is not maintained. But it looks like this would be the kinde of stuff I would prefer.

Andi
  • 888
  • 2
  • 10
  • 24