• About Us
  • Blog
  • Basket
  • Account
  • Sign In
  •  

PHP API

Installing the PHP API

Linux

First, clone 51Degrees/Device-Detection repository using git:

$ git clone https://github.com/51Degrees/Device-Detection.git

Choose the detection method you wish to use and navigate to the corresponding directory within the ‘php’ directory. For Pattern:

$ cd Device-Detection/php/pattern

For Trie:

$ cd Device-Detection/php/trie

Install by running the following commands:

phpize5
./configure
sudo make install

The phpize5 command will prepare the build environment for the PHP extension. The ./configure will check your environment and prepare the makefile. Finally the make install will build the detector module and place it in the PHP extensions directory. The build script should also tell you where the extension has been placed.

In the includes directory, there is a php file which contains all the classes needed. So put this in a directory accessible by your web server, e.g. /var/www/html/51Degrees. This can then be included at the top of any php script using the detector with

require(path/to/FiftyOneDegreesPatternV3.php);

Windows

First, clone 51Degrees/Device-Detection repository using git:

$ git clone https://github.com/51Degrees/Device-Detection.git

Now download the php source (the same version as is installed on the target system) from http://windows.php.net/download and extract to either the Pattern or Trie directory and rename to php-src.

Download the necessary binary tools from http://windows.php.net/downloads/php-sdk/ and extract the bin subfolder to either the Pattern or Trie directory. In a developer command prompt (for PHP 5.5/5.6 use Visual Studio 2012, for 7.0+ use 2015) and move to either the Pattern or Trie directory. make the binary tools available in the system path:

$ bin\phpsdk_setvars.bat

Configure and build the php source by running the following in the php-src directory:

$ cd php-sdk
$ buildconf
$ configure --disable-all --enable-cli
$ nmake

The VisualStudio directory contains a project which can now be opened and built. This will then build the dll in the VisualStudio\Release directory.

Just include the dll in a suitable PHP directory and configure as below.

Configure

Before using the extension you must supply the PHP environment with information on the location of the module and the module parameters. This should be done by editing your PHP.ini file.

If you are not sure where the PHP.ini file is located on your system you can create a .php page and place in your server directory. Add the following php code to the page

<?php
phpinfo();
?>

and look for the ‘php.ini’ file location.

Open the ‘php.ini’ file and add the following to the bottom. Please note that superuser privileges will most likely be required to edit the file.

The first is the location of the extension binary (note that your location may be different from the one here. Check for the location). The second is the is the path to the 51Degrees data file. Third parameter defines the number of WorkSet items in the pool. Make sure that it is in a location that Apache has read permissions over.

extension=/usr/lib/php5/FiftyOneDegreesPatternV3.so
FiftyOneDegreesPatternV3.data_file=path/to/51Degrees/data/file.dat
FiftyOneDegreesPatternV3.property_list=BrowserName,BrowserVendor,BrowserVersion,DeviceType,HardwareVendor,IsTablet,IsMobile,IsCrawler,ScreenInchesDiagonal,ScreenPixelsWidth
FiftyOneDegreesPatternV3.cache_size=10000
FiftyOneDegreesPatternV3.pool_size=20

In both Pattern and Trie directories, there is a php file which contains all the classes needed. So put this in a directory accessible by your web server, e.g. /var/www/html/51Degrees. This can then be included at the top of any php script using the detector with

require(path/to/FiftyOneDegreesPatternV3.php);

or

require(path/to/FiftyOneDegreesTrieV3.php);

Suggested Next Steps