php custom c extension with external shared lib - php

I would like to develop an extension which depends on an external shared library. My current problem is that I am not being able to link this shared library to my extension.
My development environment is Fedora Linux x64 + PHP 5.4. This external shared library is a proprietary one, I just have its headers (.h) and .so files (for 32 and 64 bits).
The project's current config.m4 file is as follows:
PHP_ARG_WITH(projectname,
[Whether to enable ProjectName support],
[ --with-projectname enable ProjectName support])
if test "$PHP_PROJECTNAME" != "no"; then
PHP_ADD_LIBRARY_WITH_PATH(externallib, lib64, PROJECTNAME_SHARED_LIBADD)
AC_DEFINE(HAVE_PROJECTNAME, 1, [Whether you have ProjectName])
PHP_NEW_EXTENSION(projectname, projectname.c, $ext_shared)
fi
The problem here is every time I run ./configure --with-projectname the generated makefile does not have any reference to the library, plus the compiled .so file fails (obviously).
It would also be useful if I had a way to determine the right lib directory according to the architecture.

You've included the library, but looks you're missing the actual link option. I haven't done this in the longest time, but I think something like the following should work.
EXTERNAL_LIB="blahLibName"
LIB_LINK_CMD="-L/usr/local/lib -l$EXTERNAL_LIB"
PHP_ADD_LIBRARY_WITH_PATH(externallib, lib64, PROJECTNAME_SHARED_LIBADD)
PHP_EVAL_LIBLINE($LIB_LINK_CMD, PROJECTNAME_SHARED_LIBADD)
The syntax here might not be right at all, but you definitely need the PHP_EVAL_LIBLINE.

Related

locating dependencies on windows php extension builds

I am writing a php extension for a library. I have a generic swig file to build wrappers for the library. This has been very successful so far on Python.
A user is trying to build the library for php and I am trying to help out. I generated the code using swig, and I can build the .dll extension using Visual Studio. The problem is getting it into php. When I build php_mylib.dll, it needs to find mylib.dll and it can't.
So I am trying to build via the command line a la:
http://blog.benoitblanchon.fr/build-php-extension-on-windows/
I have put all the files to be compiled and the libraries needed (i.e. mylib64.lib and mylib64.dll) in a folder called mylib in the C:\php-src\ext folder with all the other extension folders.
The problem is I that I can't get the config.w32 file to communicate the location of mylib. Here is my config.w32 file (pretty standard -- you can see that I copied it from the curl config.w32 file):
// $Id$
// vim:ft=javascript
ARG_ENABLE("mylib", "mylib support", "no");
if (PHP_MYLIB != "no") {
if (CHECK_LIB("mylib64.lib", "mylib", PHP_MYLIB) &&
CHECK_HEADER_ADD_INCLUDE("mylib_cpp.h", "CFLAGS_MYLIB")
) {
EXTENSION("mylib", "mylib_c_wrap.cpp", true);
AC_DEFINE('HAVE_MYLIB', 1, 'Have mylib library');
// TODO: check for curl_version_info
} else {
WARNING("mylib not enabled; libraries and headers not found");
}
}
When I run buildconf and then configure --disable-all --enable-cli --enable-mylib it always shoots me the 'libraries and headers not found' warning from the script.
On Unix systems (config.m4) there appears to be a PHP_ADD_LIBRARY_WITH_PATH macro but I don't see any equivalent for windows. It seems like this is what I need.
I have also tried adding the full path to mylib into the system's path but to no avail. It seems like there might be an environment variable somewhere in the PHP build cosmos that needs to be set to find external dependencies, but I can't find any information about this.
It would also be good to do all this as a Visual Studio project -- easier for Windows users the world over. I have not seen anything on the web that looks like this.
By the way, I have successfully phpized this library using the same swig+phpize procedure on Linux (I followed this guide for the php part:http://www.sitepoint.com/install-php-extensions-source/) and it works beautifully.
In most cases you can use this info to build a PECL extension https://wiki.php.net/internals/windows/stepbystepbuild#building_pecl_extensions . Your config.w32 looks ok, though please note that that when depending on some additional library, usually it should be ARG_WITH(...) for semantics. Your lib stuff can be then put into the deps dir as in the wiki.
Another way could be passing --with-extra-libs and --with-extra-includes to configure. Those have to contain paths to *.lib and *.h dirs respectively. When using a static lib, that should be it, otherwise you'll need to place the corresponding *.dll onto the %path% for the ext to work.
Hope this helps, otherwise you can also gain some attention on the PHP mailing lists or specifically on #winphp-dev at freenode.
Thanks.

Using Cassandra PDO Driver on Windows

Is there any way to have Cassandra PDO at Windows with Wamp?
This is for development purposes I don't want to install Linux and change all the environment.
https://code.google.com/a/apache-extras.org/p/cassandra-pdo/
I'm using Windows 7 (64 Bit), Wamp 2.5, PHP 5.5.
OK, here's what I found out:
1) It's totally possible
2) The docs that appear in the first google search results are a bit obsolete
Start by downloading the latest Datastax Community Cassandra here:
http://planetcassandra.org/cassandra/
Install & setup properly. In fact, most of the configuration is done by the installer, you just have to edit the apache-cassandra/conf/cassandra.yaml file to find all paths to /var/lib... and change those into something like d:/cassandra/... That includes "commitlog", "data", "saved_caches". Restart the Cassandra service, examine the logs. Mine shown no problem. The OpsCenter at ...:8888/opscenter/index.html was working fine, showing one node online.
Now, the PHP part.
There's a sneaky thing called Thrift. From what I've learned today (I first heard about Cassandra and Thrift yesterday), it's a way describe a binary protocol of connecting to some online service, in this case, to Cassandra. It will basically generate PHP files that will provide all the connectivity you need from PHP itself (no extensions needed).
You will need:
1) The Thrift PHP libs
2) The .exe Thrift compiler
Both can be downloaded here:
https://thrift.apache.org/download
Then use the following command to compile PHP files that will act as a "driver" to connect your PHP applications to Cassandra:
thrift --gen php D:\DataStaxCommunity\apache-cassandra\interface\cassandra.thrift
Put the result in some PHP include_path folder.
Also, find the PHP Thrift libs (in the libs archive from the same download page) and put those in a folder accessible to your script (e.g. include_path or the project folder).
Refer this page:
thrift.apache.org/lib/php
I guess that should help!
I have same problem as you, but when i tried this method, it works correctly for me.
Reference link
Here is a code example, very easy to understand :
<?php
require_once 'Cassandra/Cassandra.php';
$o_cassandra = new Cassandra();
$s_server_host = '127.0.0.1'; // Localhost
$i_server_port = 9042;
$s_server_username = ''; // We don't use username
$s_server_password = ''; // We don't use password
$s_server_keyspace = 'cassandra_tests';
$o_cassandra->connect($s_server_host, $s_server_username, $s_server_password, $s_server_keyspace, $i_server_port);
$s_cql = "CREATE TABLE carles_test_table (s_thekey text, s_column1 text, s_column2 text,PRIMARY KEY (s_thekey));";
$st_results = $o_cassandra->query($s_cql);

Connect PHP with SAP Database

So, I already followed all the steps below for saprfc windows installation.
1) Extract zip file saprfc-$VERSION$-$PHP_VERSION$.zip
2) Copy php-saprfc.dll to your extensions directory (e.g. C:\PHP\extensions)
3) Edit php.ini file (in windows system dircetory, e.g. C:\WINNT, C:\WINDOWS)
and add line: 'extension=php_saprfc.dll'
4) Copy librfc32.dll (from SAPGUI install CD) to the windows system
directory or simple install SAPGUI on your machine.
Another source said we will we need SAP SDK. Is it really needed for Windows?
Also, will it be possible to code PHP connect to SAP in a computer that doesn't have SAP program? Or we only can do those installations in computer that has SAP application?
Then after the installation, what do I have to do next? I am trying to do steps below, yet still confused....
1) Extract php sources to C:\PHP-x.y.z
2) Extract php win support files to C:\PHP-x.y.z\win32
3) Install RFCSDK to C:\PHP-x.y.z\rfcsdk
4) Copy SAPRFC sources to C:\PHP-x.y.z\ext\saprfc
5) Copy php4ts.lib or php5ts.lib (from PHP binaries) to C:\PHP-x.y.z\win32
6) For PHP5 copy saprfc.dsp5 to saprfc.dsp
6) Open project C:\PHP-x.y.z\ext\saprfc\saprfc.dsp in Microsoft Visual C++ 6.0
7) Under Tools|Option|Directory set path for Include files and for Library files
(C:\PHP-x.y.z\win32\include; C:\PHP-x.y.z\rfcsdk\include;
C:\PHP-x.y.z\win32\lib; C:\PHP-x.y.z\rfcsdk\lib)
8) Set active configuration to "saprfc - Windows_TS" (under Build menu)
9) Build php_saprfc.dll.
10) Compiled DLL you find in C:\PHP-x.y.z\Release_TS directory
Where to get PHP sources, PHP win support, and how to compile the program?
Sorry, I am totally new to this. Looking for the answers. Thank you very much!
Building "windows extensions" is explained in general here:
https://wiki.php.net/internals/windows/stepbystepbuild
There is also explained which version of VS you need for which PHP version.
I also tried to build the sapnwrfc extension for PHP 5.5 on windows, but failed at some part...my progress/documentation is here:
https://github.com/piersharding/php-sapnwrfc/issues/6
Maybe you can progress further :-)

Where to get SNMP class for php

I found this http://www.php.net/manual/en/class.snmp.php, but I can't find link to download this script. I tryed to search in the WEB, but the same result.
Or is the any methods with php extension to make one session and work with it?
It is in the tarball. You can download it from the official website
Take a look into it:
$ tar jtf php-5.4.13.tar.bz2 | grep snmp
php-5.4.13/ext/snmp/
php-5.4.13/ext/snmp/config.m4
php-5.4.13/ext/snmp/config.w32
php-5.4.13/ext/snmp/CREDITS
php-5.4.13/ext/snmp/php_snmp.h
php-5.4.13/ext/snmp/snmp.c
php-5.4.13/ext/snmp/snmp.dsp
php-5.4.13/ext/snmp/tests/
php-5.4.13/ext/snmp/tests/bug60749.phpt
php-5.4.13/ext/snmp/tests/bug64124.phpt
php-5.4.13/ext/snmp/tests/clean.inc
php-5.4.13/ext/snmp/tests/generic_timeout_error.phpt
php-5.4.13/ext/snmp/tests/ipv6.phpt
php-5.4.13/ext/snmp/tests/README
php-5.4.13/ext/snmp/tests/skipif.inc
php-5.4.13/ext/snmp/tests/snmp-object-errno-errstr.phpt
php-5.4.13/ext/snmp/tests/snmp-object-error.phpt
php-5.4.13/ext/snmp/tests/snmp-object-properties.phpt
php-5.4.13/ext/snmp/tests/snmp-object-setSecurity_error.phpt
php-5.4.13/ext/snmp/tests/snmp-object.phpt
php-5.4.13/ext/snmp/tests/snmp2_get.phpt
php-5.4.13/ext/snmp/tests/snmp2_getnext.phpt
php-5.4.13/ext/snmp/tests/snmp2_real_walk.phpt
php-5.4.13/ext/snmp/tests/snmp2_set-nomib.phpt
php-5.4.13/ext/snmp/tests/snmp2_set.phpt
php-5.4.13/ext/snmp/tests/snmp2_walk.phpt
php-5.4.13/ext/snmp/tests/snmp3-error.phpt
php-5.4.13/ext/snmp/tests/snmp3.phpt
php-5.4.13/ext/snmp/tests/snmp_get_quick_print.phpt
php-5.4.13/ext/snmp/tests/snmp_get_valueretrieval.phpt
php-5.4.13/ext/snmp/tests/snmp_getvalue.phpt
php-5.4.13/ext/snmp/tests/snmp_include.inc
php-5.4.13/ext/snmp/tests/snmp_read_mib.phpt
php-5.4.13/ext/snmp/tests/snmp_set_enum_print.phpt
php-5.4.13/ext/snmp/tests/snmp_set_oid_output_format.phpt
php-5.4.13/ext/snmp/tests/snmpd.conf
php-5.4.13/ext/snmp/tests/snmpget.phpt
php-5.4.13/ext/snmp/tests/snmpgetnext.phpt
php-5.4.13/ext/snmp/tests/snmprealwalk.phpt
php-5.4.13/ext/snmp/tests/snmpset-nomib.phpt
php-5.4.13/ext/snmp/tests/snmpset.phpt
php-5.4.13/ext/snmp/tests/snmpwalk.phpt
php-5.4.13/ext/snmp/tests/wrong_hostname.phpt
The "SNMP" class (introduced in PHP 5.4) is not a "script"; rather, it is an extension that is either compiled into PHP or shipped as a module that can be enabled or disabled. I suspect that either (1) you are using an older version of PHP, or (2) you do not have the SNMP extension installed or enabled.

How to make a PHP extension [duplicate]

This question already has answers here:
Where can I learn about PHP internals? [closed]
(4 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Needs details or clarity Add details and clarify the problem by editing this post.
I know you can technically make PHP extension just by making a PHP file and using require_once.
But would it optimize the performance if you wrote an extension in C or C++.
If so, how would you make a "hello-world" for that?
I know you can technically make PHP extension just by making a PHP file and using require_once.
The base of this functionality is the include statement, which includes and evaluates the specified file. Extension isn't the right term, because you are just including another PHP script file. A PHP extensions provides additional functions to the language in form of a compiled module.
But would it optimize the performance, if you wrote an extension in C or C++.
Yes, it optimizes the performance. That's why PHP extensions like CPhalcon or YAF were written.
How to make a "Hello World" PHP Extension?
I will describe how you can build a "Hello World" PHP extension in five steps.
A Debian based OS is required, because we need to fetch some tools and dependencies with apt-get.
Step 1 - Setup Build Environment / Requirements
A PHP Extension is compiled C code. We need a shell (should already be installed), an editor (your choice), a compiler (here we'll use GCC), PHP itself and PHP development dependencies for the build.
sudo apt-get install build-essential php7.0 php7.0-dev
Step 2 - Config
We need to describe our extension and the files forming it in a basic configuration file:
File: config.m4
PHP_ARG_ENABLE(php_helloworld, Whether to enable the HelloWorldPHP extension, [ --enable-helloworld-php Enable HelloWorldPHP])
if test "$PHP_HELLOWORLD" != "no"; then
PHP_NEW_EXTENSION(php_helloworld, php_helloworld.c, $ext_shared)
fi
As you can see, the NEW_EXTENSION contains a C file: php_helloworld.c.
Step 3 - Code
Let's create the C code for our extension.
Firstly, we create a header file:
php_helloworld.h
// we define Module constants
#define PHP_HELLOWORLD_EXTNAME "php_helloworld"
#define PHP_HELLOWORLD_VERSION "0.0.1"
// then we declare the function to be exported
PHP_FUNCTION(helloworld_php);
Secondly, we create the source file:
php_helloworld.c
// include the PHP API itself
#include <php.h>
// then include the header of your extension
#include "php_helloworld.h"
// register our function to the PHP API
// so that PHP knows, which functions are in this module
zend_function_entry helloworld_php_functions[] = {
PHP_FE(helloworld_php, NULL)
{NULL, NULL, NULL}
};
// some pieces of information about our module
zend_module_entry helloworld_php_module_entry = {
STANDARD_MODULE_HEADER,
PHP_HELLOWORLD_EXTNAME,
helloworld_php_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
PHP_HELLOWORLD_VERSION,
STANDARD_MODULE_PROPERTIES
};
// use a macro to output additional C code, to make ext dynamically loadable
ZEND_GET_MODULE(helloworld_php)
// Finally, we implement our "Hello World" function
// this function will be made available to PHP
// and prints to PHP stdout using printf
PHP_FUNCTION(helloworld_php) {
php_printf("Hello World! (from our extension)\n");
}
Step 4 - Build
Now, we are ready to build the extension.
First we prepare the build environment for a PHP extension:
phpize
Then we configure the build and enable our extension:
./configure --enable-php-helloworld
Finally, we can build it:
make
sudo make install
Step 5 - Test
To test our PHP extension, lets load the helloworld_php.so extension file and execute our function helloworld_php():
php -d extension=php_helloworld.so -r 'helloworld_php();'
Done :)
Building on Windows
If you try to build an Windows (YIKES!),
then you need to adjust the steps a bit:
Step 2 - Config
File: config.w32
ARG_ENABLE("helloworld", "helloworld support", "yes");
if (PHP_HELLOWORLD == "yes") {
EXTENSION("helloworld", "php_helloworld.c");
}
Step 4 - Build
Use nmake instead of make.
List of helpful resources:
A good book on the C programming language
https://wiki.php.net/internals/
https://wiki.php.net/internals/extensions
http://phpinternalsbook.com/
https://phpinternalsbook.com/php7/build_system/building_extensions.html
https://nikic.github.io/
http://jpauli.github.io/
https://github.com/php/php-src
http://www.slideshare.net/pierrej/extending-php-7-the-basics - explains basic argument handling
https://github.com/phplang/extension-tutorial - Materials for an Extension Writing Tutorial
Software written in C/C++ certainly does run faster than code in PHP. And you can write an extension in C/C++ and link it into PHP. The PHP manual covers this here: http://php.net/manual/en/internals2.php
The other answers give links to other tutorials for writing PHP extensions, and you can google for "PHP extension tutorial" to find more.
But whether this is the right thing to do in your app is another story. Most experts agree that PHP runs just fine, fast enough for 98% of applications. The instances where PHP isn't fast enough are not in general due to the language, but an inefficient application architecture that the programmer has created. That's a weakness that can't be remedied by rewriting parts of your app in C/C++.
Here's a tutorial on PHP extensions. Whether it will optimize the performance or not, it depends on what you are trying to wrap on your extension. But I would not write a PHP extension just for optimization purposes. I would write one if I have no choice. I.E. Wrapping a common C library to make it available directly in PHP...
i think (but no sure) you can do that by make an dll file and put it in ext folder which exist with php installation files.if my previous words is correct you can do that (dll) file in visual studio

Categories