/simple-opendota-php

Simple OpenDota API library for PHP

Primary LanguagePHPGNU General Public License v3.0GPL-3.0

Simple OpenDota API library for PHP

API version: 18.0.0

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.

Requirements

  • PHP >=5.3.0
  • cURL in PHP

You can use Composer to check all the requirements.

How to use

  1. Include simple_opendota.php in your project
  2. 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.

Important Notes

Additional odota_api() parameters

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 or 200 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.

Work modes

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

Setting custom callbacks

  • set_get_callback(function($url, $data) {}) for GET requests only
  • set_post_callback(function($url, $data) {}) for POST requests only
  • set_request_callback(function($url, $data, $post) {}) for all requests (recommended)

API Endpoints with multiple parameters

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).

Example

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);
});