petercoles/betfair-exchange
Composer 安装命令:
composer require petercoles/betfair-exchange
包简介
Betfair Exchange API wrapper
README 文档
README
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. 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 or Dan Cotora's very lightweight wrapper.
Installation
At the command line run
composer require petercoles/betfair-exchange
Usage
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 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 <app-key>, string <username>, string <password>);
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)
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 <app-key>, string <session-token>); string Betfair::auth()->login(string <app-key>, string <username>, string <password>);
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:
// retrieve 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 Betfair. 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
Once authenticated, betting information can be obtained from the betting subsystem. All calls have the same structure:
Betfair::betting(string <name-of-method>, array <params-for-method>);
The available methods and their parameters, mandatory or optional, are defined in the Betfair API documentation.
Account API calls follow the same pattern:
Betfair::account(string <name-of-method>, array <params-for-method>);
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
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
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.
License
This package is licensed under the MIT license.
petercoles/betfair-exchange 适用场景与选型建议
petercoles/betfair-exchange 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.36k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2016 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「betfair」 「Betfair Exchange」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 petercoles/betfair-exchange 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 petercoles/betfair-exchange 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 petercoles/betfair-exchange 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A package for validating Google Cloud's JWT provided by webhooks
A PSR-7 compatible library for making CRUD API endpoints
Fixer.io data provider for Peso
Betfair API PHP 5.4+ library
Betfair API PHP 5.4+ library
Adds Exchange Web Services Mail Support to Laravel.
统计信息
- 总下载量: 4.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-05-10