Is it possible to call C code from php - php

is it possible to call C function from 3rd party C library from a PHP5 script.
If so any link or how to do it please.

You can also write PHP extensions in C. Put very simply, an extension lets you write a function (or functions) in C, then call those functions from PHP.
This Zend article is a good introduction.

PHP is written in C, so if you have some C code that you want to be able to call in PHP, the best solution is to write a PHP extension, which will allow you to expose the functionality via some new php functions.
It's been a long time since I did this, but PHP has some tools to set up the skeleton of an extension. A good place to start is probably the official documentation

There is a way to do it, but it's probably not a very good idea. One could write a small native program around the library (an executable for your system, that is). Then, you could use the php system() function to call that program. Then, read the output you need (if any) off the standard output?
(there may be a better way, I'm not super-familliar with PHP, but at least this should work.)

If you want to execute C source-code, than it's impossible. But you can compile that code and run it from within PHP using system().

Related

Detect during compilation whether PHP is being compiled with a given extension

I am trying to build an extension for PHP. After following Sara Golemon's book I have a basic extension which I can compile as a shared module and, in addition, I can compile it statically along PHP itself.
Now I want to modify the PHP interpreter in order to intercept particular internal function invocations and communicate these calls to my extension. I want to do this only when my extension is statically compiled with PHP---the interpreter build process should otherwise generate an unmodified PHP binary. My understanding is that I should use the C preprocessor. However, to achieve my goal I need a preprocessor flag that will only be raised when PHP is configured to compile with my extension (i.e. ./configure --enable-myextension). Unfortunately, I cannot find such a flag nor one seems to be set by the configure script.
I should say here that I have tried setting preprossessor flags within my extension's code but this will not work. My extension is first touched late in the build process (i.e. roughly after the core of the interpreter) and the flags I set there are not active when the bulk of interpreter code is being compiled.
Any thoughts? Do the above sound reasonable?
My understanding is that I should use the C preprocessor.
Nope, you don't need that.
I need a preprocessor flag that will only be raised when PHP is configured to compile with my extension
Why would you want that? It would basically limit the functionality of your extension artificially, although it's possible to hook function calls no matter how your extension is compiled.
Do the above sound reasonable?
In my opinion, it's not reasonable. Please have a look at how AOP hooks function calls: https://github.com/AOP-PHP/AOP
If you need to hook more than just function calls, you need to reach down at the lowest level, the opcodes, by using zend_set_user_opcode_handler(). Please use lxr.php.net or similar tools (fgrep, etc) to find out where and how such handlers are used. I know laruence was working hard on an interesting extension last year here: http://svn.php.net/viewvc/pecl/taint/trunk/taint.c?view=markup so I would take that as the most "up to date" way of doing things as a reference, if anything has changed in the meanwhile.

Is it possible to add new functions to PHP?

I would like to add a function to PHP so I can use it in any script that runs on my server. Is it possible to register a function like if it was native from PHP? Does PHP has some kind of configuration file where I can register new global / native functions?
There is no intentions to use it in production, I'm just curious on how to achieve this.
You could use a "Global include" (defined in php.ini)
Read this (php.net manual)
auto-append-file and auto-prepend-file
You can write extensions in C or C++, in particular write or use library bindings with SWIG or FFI. But that's a bit effort, and only advisable if you meant compiled "native" functions.
The lazy option to add new core functions to PHP via config is the auto_prepend_file= php.ini setting. That allows to register a script that gets executed before everything else. (I use that for fixing magic quotes on some servers, or always having phpquery available for CLI testing.)
There is a full chapter in the manual http://www.php.net/manual/en/internals2.php devoted to writing extensions to php, with examples.
The easiest way is to write them in PHP and include them before your scripts is executed using
auto_prepend_file = /path/to/file.php
http://php.net/manual/en/ini.core.php
else you need to learn C and write a extension

calling dll through php

I need to call a dll that returns a string using PHP.
What would be the best possible way to achieve this?
Build a PHP extension that wraps the DLL or create a wrapper (in any language) that can be accessed via shell with exec.
This is not possible using native PHP.
I would look into running an operating system level function to do this using exec(), like for example rundll.exe (for some kinds of DLLs).
If rundll can't do it (it has something to do with managed and unmanaged DLLs, I don't know what that means), the easiest way may be writing a wrapper application that imports the DLL, performs the necessary actions, and outputs the result.

Making C functions available to php

I have a C dll containing functions and enumerations I want to make accessible from php.
How can I do it?
You need to write an extension. You do need some C experience but this tutorial is pretty easy to follow.
edit I googled around out of curiosity, apparently you can sort-of dynamically load a dll from php using w32api. I'd still go for the extension ;-)

Is there a tool to convert php built-in functions to c implementation?

I'm curious about how some built in functions are implemented,but it's very time consuming to look it up directly in the source,is there a tool that can automate this?
EDIT
Or is there a tool that can debug into the c code that's actually executed?
Most (all?) of the functions that can be accessed from PHP are defined under the ext/ directory in the PHP source code. If you have a recursive search tool, search for PHP_FUNCTION - if you saved the results of that search into a text file, it would be a pretty good "index" for figuring out where a PHP builtin is defined.
The really core stuff is in ext/standard.
Some rare "functions" are implemented directly as opcodes in the Zend virtual machine that PHP compiles to, so there isn't a well defined C function as such. I think strlen is such a function, for instance.
About the debugging the C code that's executed, I suppose it's possible to use something like dbg ; you'll first have to recompile PHP with the --enable-debug mode, though.
For more informations, you can take a look at :
Building PHP for extension development
Generating a gdb backtrace
I've never used this to debug PHP itself, but I've used those two pages to generate some backtraces of a crash I had with an extension, and it worked OK, from what I remember.
As a sidenote : using a PHP compiled with --enable-debug, you might have to recompile some of the extensions you're using and change the way they're loaded (it's the case for Xdebug, for instance) ; and some other might just not work at all anymore.
I believe that you should take a look at this.
Facebook has developed a tool to convert PHP code into c++.
So I guess it can handle C as well to some extent.

Categories