Client

The Client object is what sends the Guzzle request to one of the NBA endpoints and retrieves the response. The most common and recommended use is to instantiate the class without passing any arguments, but flexibility is provided to alter the base behavior through constructor arguments.

use JasonRoman\NbaApi\Client\Client;

$client = new Client(); // recommended

Guzzle Configuration Overrides

By default, a new Guzzle Client is created when instantiating the main Client. You may pass any Guzzle-specific options through the first argument of the client constructor:

$client = new Client(['proxy' => '192.168.16.1:10']);

Validation

By default, all requests sent to the client are first validated using the Symfony Validator Component. This ensures that all data you configure in your requests is of the proper data type, not null if the field is required, and contains the proper options/regular expression/data ranges. For example:

use JasonRoman\NbaApi\Client\Client;
use JasonRoman\NbaApi\Request\Stats\Stats\Game\PlayByPlayRequest;

$client  = new Client();
$request = PlayByPlayRequest::fromArray(['gameId' => '1234']);

try {
    $response = $client->request($request);
} catch (\Exception $e) {
    echo nl2br($e->getMessage());
}

Here, the $gameId parameter is incorrect. It should be 10 digits long. The error message that will be printed looks like this:

Object(JasonRoman\NbaApi\Request\Stats\Stats\Game\PlayByPlayRequest).gameId:
Param 'gameId' must match the following regex format: '/^\d{10}$/'.

While it is recommended to keep the validation enabled, you may also disable it in the constructor by passing false as the second argument:

$client = new Client([], false);

You may also disable the validation after creating the client via the setValidateRequest() function:

$client->setValidateRequest(false);

Custom Guzzle Client, Symfony Validator

This is recommended even less, but you may pass your own Guzzle Client and/or Symfony Validator as the 3rd and 4th arguments to the constructor, respectively. If you pass in your own Guzzle client, the 1st constructor parameter with custom Guzzle configuration options will be ignored. See the class definition for more details.


This site has no official affiliation with the National Basketball Association or any other 3rd-party entities listed on this site.
© 2024 Jason Roman