PHPackages                             cobwebfx/betfair-exchange - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. cobwebfx/betfair-exchange

ActiveLibrary[API Development](/categories/api)

cobwebfx/betfair-exchange
=========================

Betfair Exchange API wrapper

1.2(7y ago)18MITPHPPHP &gt;=7.0

Since May 10Pushed 7y ago1 watchersCompare

[ Source](https://github.com/cobwebfx/Betfair-Exchange)[ Packagist](https://packagist.org/packages/cobwebfx/betfair-exchange)[ Docs](https://github.com/petercoles/betfair-exchange)[ RSS](/packages/cobwebfx-betfair-exchange/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (16)Used By (0)

Betfair Exchange API Wrapper
============================

[](#betfair-exchange-api-wrapper)

[![SensioLabsInsight](https://camo.githubusercontent.com/0a56c04e5332c10a31dd64e453c8681f44eacd28dbe025853cec0d8935f82df2/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f31623234373430652d356239312d343637652d386434342d3361326331353866616661612f6d696e692e706e67)](https://insight.sensiolabs.com/projects/1b24740e-5b91-467e-8d44-3a2c158fafaa)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b250f987262dcfe1ed063bf9c72c1137b62fb15c0f0df2a851a0c8eaada50281/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7065746572636f6c65732f426574666169722d45786368616e67652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/petercoles/Betfair-Exchange/?branch=master)[![License](https://camo.githubusercontent.com/e498eead712d82d9ee1af0a4850acd2e46ea48c48fb9ce5a3d2ab64f28f95b34/687474703a2f2f696d672e736869656c64732e696f2f3a6c6963656e73652d6d69742d626c75652e737667)](http://doge.mit-license.org)

Introduction
------------

[](#introduction)

This is a simple wrapper to the Betfair Exchange API. It's main role is to simplify the initiation and maintenance of Betfair sessions. It requires that you understand the requests that the Betfair API accepts and the parameters that each uses. These can be found on the [Betfair Developer Site](http://docs.developer.betfair.com/docs). These can be a bit daunting at first, but when used for a while, the underlying structure becomes clear.

If this isn't to your taste, then you might like to consider [Daniel D'Angeli's full blooded package](https://github.com/danieledangeli/betfair-php) or [Dan Cotora's very lightweight wrapper](https://github.com/dcro/simple-betfair-php-api).

Installation
------------

[](#installation)

At the command line run

```
composer require petercoles/betfair-exchange
```

Usage
-----

[](#usage)

### Authenticating a Single Process (simple)

[](#authenticating-a-single-process-simple)

Currently the package has been tested against the UK exchange only. Most methods are now in place, but order placement and similar still need testing.

You will need to have a [Betfair developer account](https://developer.betfair.com/) with that Exchange and have obtained a APP\_KEY from it - there's no charge for this, or indeed using the API for personal use.

Each Betfair request starts with a static call to one of the packages subsystems. There are three of these: "auth", "account" and "betting".

All requests require to you be logged in to the Betfair Exchange, otherwise it won't talk to you. So your first call will always be:

```
Betfair::init(string , string , string );
```

On a first call it will store your APP\_KEY, then login and retrieve a SESSION\_TOKEN and finally store that token. The app key and session token will be used to authenticate all subsequent requests. It's safe, indeed recommended, to call init() before each group of requests. If there's an active session already, it will simply extend this session rather than logging in again.

Authentication information will only exist for the current process (i.e. web page request or CLI command) has completed, but will be available for as many Betfair accesses as you attempt within that request.

### Authenticating Multiple Processes (recommended)

[](#authenticating-multiple-processes-recommended)

To share the the authentication credentials across multiple requests, e.g. avoid the need to login on each ajax request, the package offers the following options:

```
string Betfair::auth()->persist(string , string );
string Betfair::auth()->login(string , string , string );
```

This assumes that you are storing the credentials in a PHP session, a cache or a database, indeed anywhere where it can be accessed by a request and passed as a parameter to this method.

An effective approach for this would be:

```
// retreive Betfair session token from cache and if it doesn't exist ...
// ... login and get one and save it to the cache
if (!($token = Cache::get('betfairToken'))) {
    $token = Betfair::auth()->login(
        config('betfair.app-key'), config('betfair.username'), config('betfair.password')
    );
    Cache::put('betfairToken', $token, \PeterColes\Betfair\Api\Auth::SESSION_LENGTH);
}

// Persist the application key and session token, which means make them class variables
// in a singleton class and therefore available for the life of the script.
Betfair::auth()->persist(config('betfair.app-key'), $token);
```

(this example uses Laravel's caching and config features - substitute an approach appropriate for your application).

If a null session token is received, the package will not make a call to Betafir. Instead it will immediately throw an exception that can be caught (as in the example above) to login and obtain a token that can be persisted for subsequent requests. This can be useful for the first request, or subsequent requests where the token may have expired.

Three other authentication methods are available, though it's unlikely that you'll need to use them

```
. logout();
. keepAlive();
. sessionremaining();
```

If you're manually managing your Betfair sessions, you could use the keep alive method to extend your Betfair session, though the persist method described above is usually a better soltion. Betfair sessions for the UK exchange are currently 4 hours long.

### Obtaining Data

[](#obtaining-data)

Once authenticated, betting information can be obtained from the betting subsystem. All calls have the same structure:

```
Betfair::betting(string , array );
```

The available methods and their parameters, mandatory or optional, are defined in the [Betfair API documentation](https://developer.betfair.com/exchange-api/).

Account API calls follow the same pattern:

```
Betfair::account(string , array );
```

Example
-------

[](#example)

To make this real, here's a simple example, where we'll first initialise a connection to the API and then request a list of all current events listed for the Italian Serie A soccer league:

```
Betfair::init('BetfairAppKeyHere', 'you@example.com', 'your-password');
$events = Betfair::betting('listEvents', ['filter' => ['textQuery' => 'Serie A']]);
```

Testing
-------

[](#testing)

The package comes with two test suites (or at least will do). The "unit" test suite (when written) simulate http activity (i.e. won't hit the Betfair servers), however the "integration" test suite will test connectivity and the acceptability of requests so will need valid credentials.

A valid Betfair account for testing is not provided by this package! so to run the tests rename the phpunit.xml.dist to phpunit.xml and edit it to add your Betfair, app key, username and password.

To run the test suites:

```
phpunit --testsuite=unit (not yet available)
phpunit --testsuite=integration
```

It's recommended that you only run the tests via the test suites, as some tests are deliberately excluded to avoid unintended placement of orders or movement of funds.

Issues
------

[](#issues)

This package was developed to meet a specific need and then generalised for wider use. If you have a use case not currently met, or see something that appears to not be working correctly, please raise an issue at the [github repo](https://github.com/petercoles/betfair-exchange/issues).

License
-------

[](#license)

This package is licensed under the [MIT license](http://opensource.org/licenses/MIT).

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 98.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~70 days

Recently: every ~140 days

Total

13

Last Release

2805d ago

Major Versions

0.2.1 → 1.0.02017-02-21

PHP version history (2 changes)0.0.0PHP &gt;=5.6.0

1.0.0PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/1727b4399e30f0f46ba2af9edd6addefc05bfc60338acb678086a61101993aa3?d=identicon)[cobwebfx](/maintainers/cobwebfx)

---

Top Contributors

[![petercoles](https://avatars.githubusercontent.com/u/2947594?v=4)](https://github.com/petercoles "petercoles (109 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")[![Toyinster](https://avatars.githubusercontent.com/u/2857044?v=4)](https://github.com/Toyinster "Toyinster (1 commits)")

---

Tags

apibetfairBetfair Exchange

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cobwebfx-betfair-exchange/health.svg)

```
[![Health](https://phpackages.com/badges/cobwebfx-betfair-exchange/health.svg)](https://phpackages.com/packages/cobwebfx-betfair-exchange)
```

###  Alternatives

[openai-php/laravel

OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with the Open AI API

3.7k7.6M74](/packages/openai-php-laravel)[petercoles/betfair-exchange

Betfair Exchange API wrapper

144.4k](/packages/petercoles-betfair-exchange)[get-stream/stream-chat

A PHP client for Stream Chat (https://getstream.io/chat/)

301.8M2](/packages/get-stream-stream-chat)[mvdnbrk/dhlparcel-php-api

DHL Parcel API client for PHP

3957.9k5](/packages/mvdnbrk-dhlparcel-php-api)[erlangb/betfair

Betfair API PHP 5.4+ library

471.6k](/packages/erlangb-betfair)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
