Canvas
in package
Canvas facade class for making raw API calls to arbitrary Canvas URLs.
This class provides a clean, intuitive interface for making direct API calls to Canvas URLs. It's particularly useful for:
- Following pagination URLs returned by Canvas
- Calling custom or undocumented endpoints
- Processing webhook callbacks with embedded URLs
- Following URLs in API responses (e.g., file downloads)
- Accessing beta or experimental Canvas features
Tags
Table of Contents
Properties
- $httpClient : HttpClientInterface|null
Methods
- delete() : mixed
- Make a DELETE request to a Canvas URL
- get() : mixed
- Make a GET request to a Canvas URL
- patch() : mixed
- Make a PATCH request to a Canvas URL
- post() : mixed
- Make a POST request to a Canvas URL
- put() : mixed
- Make a PUT request to a Canvas URL
- request() : mixed
- Make a request to a Canvas URL with any HTTP method
- setHttpClient() : void
- Set a custom HTTP client instance
- getHttpClient() : HttpClientInterface
- Get the HTTP client instance, creating one if necessary
- isMultipartData() : bool
- Check if data should be sent as multipart
- parseResponse() : mixed
- Parse the response based on content type
- prepareMultipartData() : array<string|int, mixed>
- Prepare data for multipart encoding
- flattenArray() : void
- Recursively flatten nested arrays into multipart format
Properties
$httpClient
protected
static HttpClientInterface|null
$httpClient
= null
The HTTP client instance
Methods
delete()
Make a DELETE request to a Canvas URL
public
static delete(string $url[, array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $url : string
-
Full URL or relative path
- $options : array<string|int, mixed> = []
-
Optional Guzzle request options
Tags
Return values
mixed —Decoded JSON response or raw response based on content type
get()
Make a GET request to a Canvas URL
public
static get(string $url[, array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $url : string
-
Full URL or relative path
- $options : array<string|int, mixed> = []
-
Optional Guzzle request options
Tags
Return values
mixed —Decoded JSON response or raw response based on content type
patch()
Make a PATCH request to a Canvas URL
public
static patch(string $url[, array<string|int, mixed>|null $data = null ][, array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $url : string
-
Full URL or relative path
- $data : array<string|int, mixed>|null = null
-
Data to send in request body
- $options : array<string|int, mixed> = []
-
Optional Guzzle request options
Tags
Return values
mixed —Decoded JSON response or raw response based on content type
post()
Make a POST request to a Canvas URL
public
static post(string $url[, array<string|int, mixed>|null $data = null ][, array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $url : string
-
Full URL or relative path
- $data : array<string|int, mixed>|null = null
-
Data to send in request body
- $options : array<string|int, mixed> = []
-
Optional Guzzle request options
Tags
Return values
mixed —Decoded JSON response or raw response based on content type
put()
Make a PUT request to a Canvas URL
public
static put(string $url[, array<string|int, mixed>|null $data = null ][, array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $url : string
-
Full URL or relative path
- $data : array<string|int, mixed>|null = null
-
Data to send in request body
- $options : array<string|int, mixed> = []
-
Optional Guzzle request options
Tags
Return values
mixed —Decoded JSON response or raw response based on content type
request()
Make a request to a Canvas URL with any HTTP method
public
static request(string $url[, string $method = 'GET' ][, array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $url : string
-
Full URL or relative path
- $method : string = 'GET'
-
HTTP method (GET, POST, PUT, DELETE, PATCH, etc.)
- $options : array<string|int, mixed> = []
-
Optional Guzzle request options
Tags
Return values
mixed —Decoded JSON response or raw response based on content type
setHttpClient()
Set a custom HTTP client instance
public
static setHttpClient(HttpClientInterface|null $client) : void
Parameters
- $client : HttpClientInterface|null
-
Pass null to reset to default
getHttpClient()
Get the HTTP client instance, creating one if necessary
protected
static getHttpClient() : HttpClientInterface
Return values
HttpClientInterfaceisMultipartData()
Check if data should be sent as multipart
protected
static isMultipartData(array<string|int, mixed> $data) : bool
Canvas API uses multipart for certain types of data, particularly when arrays are nested or when file uploads are involved.
Parameters
- $data : array<string|int, mixed>
Return values
boolparseResponse()
Parse the response based on content type
protected
static parseResponse(ResponseInterface $response) : mixed
Parameters
- $response : ResponseInterface
prepareMultipartData()
Prepare data for multipart encoding
protected
static prepareMultipartData(array<string|int, mixed> $data) : array<string|int, mixed>
This method recursively flattens nested arrays into Canvas API's expected multipart format. It handles:
- Scalar values: converted to strings
- Resources: preserved for file uploads
- Sequential arrays: appended with [] suffix (e.g., field[]=value1, field[]=value2)
- Associative arrays: nested with [key] notation (e.g., field[subfield]=value)
- Deeply nested structures: recursively processed to arbitrary depth
Parameters
- $data : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>flattenArray()
Recursively flatten nested arrays into multipart format
private
static flattenArray(mixed $data, string $prefix, array<string|int, mixed> &$result) : void
This helper method is called by prepareMultipartData() to recursively process nested data structures and convert them into the flat multipart format expected by Canvas API endpoints.
Sequential arrays of scalars get [] suffix: colors[]=red, colors[]=blue Sequential arrays of arrays get numeric indices: items[0][name]=A, items[1][name]=B
Parameters
- $data : mixed
-
The data to flatten (can be scalar, array, or resource)
- $prefix : string
-
The current field name prefix (e.g., "user[address]")
- $result : array<string|int, mixed>
-
Reference to the result array being built