Giter Site home page Giter Site logo

browscap's Introduction

Browscap Parsing Class

Introduction

Crossjoin\Browscap allows to check for browser settings based on the user agent string, using the data from Browscap (the Browser Capabilities Project).

Although PHP has the native get_browser() function to do this, this implementation offers some advantages:

  • The PHP function requires to set the path of the browscap.ini file in the php.ini directive browscap, which is flagged as PHP_INI_SYSTEM (so it can only be set in php.ini or httpd.conf, which isn't allowed in many cases, e.g. in shared hosting environments).
  • It's much faster than the PHP function (between 20-50 times, depending on the PHP version, the searched user agent and other factors)
  • It includes automatic updates of the Browscap source
  • All components are extendible - use your own parser, updater, formatter or cache functionality

Requirements

  • PHP 5.3+ (it has been successfully tested with PHP 5.3.28 - PHP 5.6.0beta3, perhaps also older versions still work)
  • PHP 5.5+ recommended (to be able to use generators, which reduces memory consumption a lot)
  • For automatic updates: cURL extension or allow_url_fopen enabled in php.ini,

Simple example

<?php
$browscap = new \Crossjoin\Browscap\Browscap();
$settings = $browscap->getBrowser()->getData();

Advanced example

<?php
// set an own cache directory (otherwise the system temp directory is used)
\Crossjoin\Browscap\Cache\File::setCacheDirectory(__DIR__ . DIRECTORY_SEPARATOR . 'tmp');

// disable automatic updates
$updater = new \Crossjoin\Browscap\Updater\None();
\Crossjoin\Browscap\Browscap::setUpdater($updater);

// set local updater that extends \Crossjoin\Browscap\Updater\AbstractUpdater or 
// \Crossjoin\Browscap\Updater\AbstractUpdaterRemote
$updater = new \Crossjoin\Browscap\Updater\Local();
$updater->setOption('LocalFile', __DIR__ . DIRECTORY_SEPARATOR . 'browscap.ini');
\Crossjoin\Browscap\Browscap::setUpdater($updater);

// set HTTP proxy server (without authentication)
//$updater->setOptions(array(
//    'ProxyProtocol' => \Crossjoin\Browscap\Updater\AbstractUpdaterRemote::PROXY_PROTOCOL_HTTP,
//    'ProxyHost'     => '79.143.82.242',
//    'ProxyPort'     => '8080',
//));

// set HTTPS proxy server (with HTTP Basic authentication, the default mode - NOT TESTED YET!)
//$updater->setOptions(array(
//    'ProxyProtocol' => \Crossjoin\Browscap\Updater\AbstractUpdaterRemote::PROXY_PROTOCOL_HTTPS,
//    'ProxyHost'     => '23.23.74.33',
//    'ProxyPort'     => '80',
//    'ProxyUser'     => 'user',
//    'ProxyPassword' => 'p4ssw0rd',
//));

// set HTTPS proxy server (with NTLM authentication, for cURL updater only - NOT TESTED YET!)
//$updater->setOptions(array(
//    'ProxyProtocol' => \Crossjoin\Browscap\Updater\AbstractUpdaterRemote::PROXY_PROTOCOL_HTTPS,
//    'ProxyHost'     => '23.23.74.33',
//    'ProxyPort'     => '80',
//    'ProxyAuth'     => \Crossjoin\Browscap\Updater\AbstractUpdaterRemote::PROXY_AUTH_NTLM,
//    'ProxyUser'     => 'user',
//    'ProxyPassword' => 'p4ssw0rd',
//));

// set an own parser implementation that extends \Crossjoin\Browscap\Parser\AbstractParser 
// (also for other formats than INI)
//$parser = new \My\Browscap\Parser\Ini();
//\Crossjoin\Browscap\Browscap::setParser($parser);

// set an own formatter that extends \Crossjoin\Browscap\Formatter\AbstractFormatter
//$formatter = new \My\Browscap\Formatter\Extended();
//\Crossjoin\Browscap\Browscap::setFormatter($formatter);

$browscap = new \Crossjoin\Browscap\Browscap();
$settings = $browscap->getBrowser()->getData();

Background

I created Crossjoin\Browscap because I searched for a good Browscap parser class.

First I worked with the Browscap-PHP class, but it required a lot of memory and wasn't really extendible. After contributing some optimizations to the project, I realized that I need to build the parser from scratch, to be able to further optimize it.

This is the result, published as a separate project as it's not compatible to Browscap-PHP:

  • No more direct access to object properties
  • New default format (to be a real replacement of PHP get_browser(): object instead of an array, lower-case keys by default
  • Some missing features (see below)

Things to do...

  • Update via fsockopen isn't possible (possible in Browscap-PHP)

Issues and feature requests

Please report your issues and ask for new features on the GitHub Issue Tracker: https://github.com/crossjoin/browscap/issues

Please report incorrectly identified User Agents and browser detect in the browscap.ini file to Browscap: https://github.com/browscap/browscap/issues

browscap's People

Contributors

cziegenberg avatar markchalloner avatar hogewind avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.