GithubHelp home page GithubHelp logo

smartnetguru / browscap-php Goto Github PK

View Code? Open in Web Editor NEW

This project forked from browscap/browscap-php

0.0 2.0 0.0 2.33 MB

Officially supported Browscap for PHP

Home Page: http://browscap.org/

License: MIT License

Shell 0.08% PHP 99.92%

browscap-php's Introduction

Browser Capabilities PHP Project

This is a userland replacement for PHP's native get_browser() function, which is officially supported by the Browser Capabilities Project.

Note that you are currently viewing the unstable master branch (will be 3.x versions). If you're installing via composer require browscap/browscap-php, depending on your minimum stability, you may be using the latest version 2.x, so please read the documentation for that branch here.

Build Status Code Coverage Scrutinizer Quality Score

Installation

Run the command below to install via Composer

composer require browscap/browscap-php 

Then you may identify the current user agent like so:

use BrowscapPHP\Browscap;

$browscap = new Browscap();
$info = $browscap->getBrowser();

Recommended Setup

Before you can start, you have to download the browscap.ini file and convert it into a cache. There are two ways.

  1. Download the file and convert it in two steps. The downloaded file will be stored in a local file, but there is no check if the remote file has changed. If your cache gets corrupted you only need to rerun the convert command.
vendor/bin/browscap-php browscap:fetch
vendor/bin/browscap-php browscap:convert
  1. Download the file and convert it in one step. The downloaded file will not be stored in a local file, but there is a check if the remote file has changed. If your cache gets corrupted you have clean the cache and restart the process.
vendor/bin/browscap-php browscap:update

If you only want to check if a new version of the browscap.ini is available, you can use the browscap:check-update command.

Note: Both ways to create/update the cache and also checking the update will use the standard mode file as default. If you want more detailed information you may change this with the remote-file option. Please use the help function this parameter.

vendor/bin/browscap-php browscap:update --remote-file Full_PHP_BrowscapINI

Each operation expect fetch uses a cache inside the resources directory inside the project. If you update this library with composer, the cache is cleared also. If you want to avoid this and want to set your own cache folder, you can use the cache option. If you do this, you have to set a Cache Instance for this this path (see below).

vendor/bin/browscap-php browscap:update --cache ./browscap-cache

Note: Each operation (fetch, update, check-update) which fetches data from the remote host browscap.org may run into the rate limit of that site. If this happens an Exception is thrown.

A sample using composer with taking the useragent from the global $_SERVER variable.

require 'vendor/autoload.php';

// The Browscap class is in the BrowscapPHP namespace, so import it
use BrowscapPHP\Browscap;

// Create a new Browscap object (loads or creates the cache)
$bc = new Browscap();

// Get information about the current browser's user agent
$current_browser = $bc->getBrowser();

If you have an user agent you can change the function

$current_browser = $bc->getBrowser($the_user_agent);

If you want to log something that happens with the detector you may set an logger. This logger has to implement the logger interface from Psr\Log\LoggerInterface

$bc = new Browscap();
$bc->setLogger($logger);
$current_browser = $bc->getBrowser();

If you want to use an other cache than the file cache, you may set a different one. You have to change the cache adapter before building the cache with the convert or the update commands.

NOTE: If you want to use a different cache, the samples above will not work, because they are using a predefined file cache

This cache adapter has to implement the adapter interface from WurflCache\Adapter\AdapterInterface

$adapter = new \WurflCache\Adapter\Memcache(<your memcache configuration as array>);
$bc = new Browscap();
$bc->setCache($adapter);
$current_browser = $bc->getBrowser();

In this sample a memcache is used to store the full version of the bropwscap.ini file

$adapter = new \WurflCache\Adapter\Memcache(<your memcache configuration as array>);

$bc = new Browscap();
$bc
    ->setCache($adapter)
    ->update(\BrowscapPHP\Helper\IniLoader::PHP_INI_FULL)
;

If you are behind a proxy or need a spcific configuration, you have to set up a client instance. See into the Guzzle documentation for more information about this.

$client = new \GuzzleHttp\Client($options = []);

$bc = new Browscap();
$bc->setClient($client);

Issues and feature requests

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

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

browscap-php's People

Contributors

mimmi20 avatar asgrim avatar garetjax avatar scrutinizer-auto-fixer avatar quentin389 avatar dominikto avatar daawesomep avatar tmolitor-stud-tu avatar vitorbrandao avatar thadafinser avatar cziegenberg avatar joshuaestes avatar jonathaningram avatar frederikbosch avatar bassemn avatar wanderingzombie avatar smatyas avatar soullivaneuh avatar erunion avatar

Watchers

James Cloos avatar Smart Segun 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.