Simple OpenDota API support for PHP.
Handles API cooldown (to not get banned), API key usage, usage of various instances of OpenDota, Cli reporting.
It's sync only version with weird methods naming. It's intended to be used for simple PHP scripts (cli ones in the first place). For Async version made for ReactPHP look for reactive-opendota-php.
Note: It doesn't have an implementation for /feed endpoint. The endpoint tends to be unstable and it also requires HTTP streams support which doesn't work well with sync PHP.
- PHP >=5.3.0
- cURL in PHP
You can use Composer to check all the requirements.
- Include
simple_opendota.php
in your project - Create
new odota_api()
instance.
That's it. You can work with OpenDota API through odota_api
methods. Every method returns associative array made out of JSON response.
You can find the list of methods and their API counterparts in ENDPOINTS.md.
Full version: odota_api($cli_report_status, $hostname, $cooldown, $api_key)
$cli_report_status
: (bool) report about every action to console. Default:false
$hostname
: (string) address of OpenDota API instance you're going to work with. Default:"https://api.opendota.com/api/"
$cooldown
: (int) API's cooldown between requests in milliseconds. Default:1000
or200
if API key was specified (approximately 1 per second)$api_key
: (string) OpenDota API Key. Default: none
If you need to skip one of parameters, you can left it empty for default value.
Every method's last parameter is $mode
. It may have one of three values:
0
: Safe mode, sleep until API is ready (default)1
: Force mode, don't wait for API cooldown-1
: Fast mode, drop request if API isn't ready
set_get_callback(function($url, $data) {})
for GET requests onlyset_post_callback(function($url, $data) {})
for POST requests onlyset_request_callback(function($url, $data, $post) {})
for all requests (recommended)
API Endpoints with multiple GET parameters (for example, /players/{account_id}/matches
) use additional parameter:
$params
: (array) array of parameters. Every key is parameter's name, every value is its value.
It's second to last for the methods of those endpoints. Parameters names are directly translated to API endpoint, so if you'll need them, just check OpenDota Docs and use the same names.
To see what methods use $params
array and what don't, check ENDPOINTS.md (or you can check the code itself).
require "simple_opendota.php";
$od = new \SimpleOpenDotaPHP\odota_api();
$res = $od->match(1234567902);
$od = new odota_api(true);
$res = $od->player(123123123);
$od = new odota_api(true, "localhost", 100);
$res = $od->live();
$od->set_get_callback(function($url, $data) {
return $url.' '.json_encode($data);
});
$od->set_request_callback(function($url, $data, $post) {
return 'R '.$url.' '.json_encode($data);
});