How do I enable XSLT functions in PHP 5? - php

I'm wanting to print an xml file out as an HTML nested list using xslt, and as far as I know the code is correct, however I'm getting this error
Fatal error: Call to undefined function xslt_create()
Which I presume means the xslt functions havn't been enabled. How do I enable these functions within PHP? Is there a php file I need to include (like the way Javascript libraries work) or is it something more complicated? I'm hosted with MediaTemple.
Here's the php code I'm using:
<?php
// Allocate a new XSLT processor
$xh = xslt_create();
// Process the document, returning the result into the $result variable
$result = xslt_process($xh, 'armstrong.xml', 'familyToNestedList.xsl');
if ($result) {
print "SUCCESS, sample.xml was transformed by sample.xsl into the \$result";
print " variable, the \$result variable has the following contents\n<br>\n";
print "<pre>\n";
print $result;
print "</pre>\n";
}
else {
print "Sorry, sample.xml could not be transformed by sample.xsl into";
print " the \$result variable the reason is that " . xslt_error($xh) .
print " and the error code is " . xslt_errno($xh);
}
xslt_free($xh);
?>
Thanks in advance!

Depending on your distro of linux, you may be able to just install the php5-xsl module, add the line "extension=php_xsl.so" to your php.ini file, and restart your apache2 server.
This worked for me on Ubuntu 11.10. You can input "sudo apt-get install php5-xsl" into the command line to install php5-xml.

You have to compile PHP with the support for it and make sure to have all the required dependencies. Please see the corresponding chapters in the PHP Manual for XSLT.
From chapter Requirements
This extension requires the libxml PHP extension. This means that passing in --enable-libxml is also required, although this is implicitly accomplished because libxml is enabled by default. This extension uses Sablotron and expat, which can both be found at » http://freshmeat.net/projects/sablotron/. Binaries are provided as well as source. Enable by using the --with-xslt option with PHP 4.
From chapter Installation
On Unix, run configure with the --enable-xslt --with-xslt-sablot options. The Sablotron library should be installed somewhere your compiler can find it. Make sure you have the same libraries linked to the Sablotron library as those, which are linked with PHP. The configuration options: --with-expat-dir=DIR --with-iconv-dir=DIR are there to help you specify them. When asking for support, always mention these directives, and whether there are other versions of those libraries installed on your system somewhere. Naturally, provide all the version numbers.
The recommended extension for using XSL transformations with PHP5 is XSL. If all you need to do is transform two documents, as show in your example, consider this example from the PHP Manual:
$xml = new DOMDocument;
$xml->load('collection.xml');
$xsl = new DOMDocument;
$xsl->load('collection.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($xml);
The transformToXML method will return the transformed document or FALSE, so you can keep the if/else from your code. In any case, it should be trivial to upgrade your code.

Well, is very simple, but on php.net its could be explained more explicitly.
I'm using one docker container contain ubuntu base and using php-fpm.
The steps to install this extension in my context were:
First search xsl extension on linux repository
apt-cache search xsl
I ended up finding the php5-xsl, so it was only install
apt-get install php5-xsl
that the installation process the setup configuration is already added, if does not happen, just make yourself
vim /etc/php5/mods-available/xsl.ini
insert this content:
extension=xsl.so
(obviously the paths are according to your php configuration settings, but my example is the default configuration)
Restart you php fpm and done!

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.

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.

PHP Phar created with Phar::createDefaultStub running as CLI in browser

I have an application I want to package as a PHAR with both CLI and web interfaces (it acts as an API to wider system so I'd like to avoid reproducing the same libraries across two PHAR archives). The 'bug' im encountering is that the PHAR always acts like it's run from the command line when created with createDefaultStub.
<?php
try {
$phar = new Phar('myphar.phar');
$phar['cli.php'] = '<?php echo "CLI"; ?>';
$phar['web.php'] = '<?php echo "Web"; ?>';
$phar->setDefaultStub('cli.php', 'web.php');
// this is the same as:
// $phar->setStub($phar->createDefaultStub('cli.php', 'web/index.php'));
} catch (Exception $e) {
// handle errors
}
?>
If I run the phar created from the above code which was taken directly from (http://www.php.net/manual/en/phar.createdefaultstub.php from the command line with
php.exe myphar.phar
I see, "CLI" as expected, but I also see this from the web also.
There is a documented bug from 2010, but since it appears unsolved and not many cases available I'm wondering if its something I'm doing wrong at my end.
PHP Bug Report: https://bugs.php.net/bug.php?id=52322&edit=2
Things I've done to try and diagnose:
Tested on PHP 5.4.11, 5.3.21, 5.2.17
The phar was recreated under each version also.
Tried on a clean install of the latest XAMPP.
PHP.ini settings
detect_unicode = Off
phar.readonly = Off
phar.require_hash = Off
Debugged with echo from cli.php of:
$_SERVER['REQUEST_URI']
$_SERVER['REQUEST_METHOD']
Both return as expected, these are the values used in the default stub to check if it's running on the web.
I really am at a loss here and wondered if anybody had ideas about where to turn next?
Writing a custom stub is an option, but likely something is going wrong behind the scenes that will trip me up later on.
Any advice would be greatly appreciated :)

Temporarily disable cURL for testing purposes

I am writing a class that detects whether cURL is available, does one thing if it is, and another if it isn't. I therefore need to know how to disable cURL temporarily to test this class. I do not want to change the PHP INI file. Any ideas much appreciated.
Just wondering, Im writing an alternative for if cURL is unavailble, how likely is this? Am I wasting my time. Is cURL usually available?
Curl is enabled / disabled in your php.ini. You can't enable and disable it any other way.
Open php.ini find the below and put a semi colon before it to comment it out:
extension=php_curl.dll
AFAIK there is no way to do this at run time, because modules are loaded during PHP startup, before any of you code is executed. The only way to do it is by disabling (or rather, not enabling) an extension in php.ini. You probably can't even do that with cURL, as it will probably be compiled in, not dynamically loaded.
Having said that - why not just change the check to test your "if not available" code - presumably you have a block something like this:
if (curl_exists()) { //obviously there is no such function, but you must have some condition that determines this
// Do stuff using curl
} else {
// Do something horrible
}
well, just change it to this temporarily:
if (!curl_exists()) {
// etc etc
I think the best option is to change your detection script to allow disabling it with a manual configuration.
You cannot disable function on the fly. You need to change php.ini for that.
http://www.php.net/manual/en/function.dl.php
dl — Loads a PHP extension at runtime
bool dl ( string $library )
Loads the PHP extension given by the parameter library.
Use extension_loaded() to test whether a given extension is already available or not. This works on both built-in extensions and dynamically loaded ones (either through php.ini or dl()).
Warning:
This function has been removed from some SAPI's in PHP 5.3.
<?php
// Example loading an extension based on OS
if (!extension_loaded('sqlite')) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
dl('php_sqlite.dll');
} else {
dl('sqlite.so');
}
}
//this deals with sqlite but would be easy to figure out how to use it for cURL :)
?>
So you can comment out the loading of cURL extension in php.ini and then "dynamically load" it when needed.
HTH
probably the easiest way is by open file curl.ini, Im use ubuntu 12.04 and file located at
/etc/php5/apache2/conf.d/curl.ini
leave a comment by adding semicolon before extension=curl.so
You can see the location of curl.ini through phpinfo ();
dont forget to restart the Apache
sudo service apache2 restart
Curl is available as long its extension is loaded (which is mostly by default).
You can check what curl extension provides by the following command:
php --re curl
which gives you list of functions, classes and its methods.
To temporary disable curl extension, you can run PHP with -n to simply ignore your php.ini, for example:
$ php -n -r "print_r(curl_version());"
Fatal error: Call to undefined function curl_version() in Command line code on line 1
Here is working example:
$ php -r "print_r(curl_version());"
Array
(
[version_number] => 463623
...

Load php extensions dynamically

I have a live server which I want to occasionally use for testing purposes. I only have access to FTP and some basic administration tools there.
Reading the documentation for dl() gives me hope I can load xDebug dynamically even though I can't add it to the loaded extension list. I have little idea how though.
Question: How to obtain the appropriate compiled version of xdebug (or any other PHP extension) which would be ready to be used with dl()?
BTW, AFAIK the OS is CentOS 4 in my case, but I'd appreciate a broader answer too - for future reference.
xdebug is a zend-engine extension and thus cant be loaded dynamically.
You can try with xhprof instead. That should be possible to load at run time (I haven't much experience with it though, so i cant offer you specifics)
I usually use php_uname to determine the server OS
function os_check() {
$os_string = php_uname('s');
if (strpos(strtoupper($os_string), 'WIN')!==false) {
return 'windows';
} else {
return 'linux';
}
Such information is in various places in phpInfo()
<?php
phpinfo();
?>

Categories