All requests require two things - a Client object and a Request object. The Client is what polls data from one of the NBA endpoints, and the Request object is the type of data you want to retrieve. The easiest way to get started is with a simple example:


require_once 'vendor/autoload.php'; // skip this if using a framework / autoloading elsewhere

use JasonRoman\NbaApi\Client\Client;
use JasonRoman\NbaApi\Request\Data\Prod\General\TodayRequest;

$client = new Client();

$request  = TodayRequest::fromArray();
$response = $client->request($request);

That's it! The request is retrieved via Guzzle and wrapped in a response class that provides the original Guzzle response as well as some helper functions.

Data Processing Helper Functions

The returned response object contains several helper functions for common tasks, like converting the response to an array or object. These functions are listed below, with examples.

Convert a JSON response to an array:

$arrayResponse = $response->getArrayFromJson();

Convert a JSON response to an object (including sub-objects):

$objectResponse = $response->getObjectFromJson();

Similar to the object response, but may return an array at the top-level if there are no key/value pairs:

$objectPropertiesResponse = $response->getObjectPropertiesFromJson();

Convert an XML response to a \SimpleXMLElement object:

$xmlResponse = $response->getXml();

Retrieve the original Guzzle Response object:

$guzzleResponse = $response->getResponse();

Retrieve the original Guzzle response body as a string:

$responseBody = $response->getResponseBody();

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