Documentation

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
example
// Following pagination
$nextPage = Canvas::get($courses->getNextUrl());

// Custom endpoint
$analytics = Canvas::get('/api/v1/custom/analytics');

// Create resource
$result = Canvas::post('/api/v1/courses/123/custom_resources', [
    'name' => 'My Resource'
]);

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

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
throws
CanvasApiException
throws
MissingApiKeyException
throws
MissingBaseUrlException
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
throws
CanvasApiException
throws
MissingApiKeyException
throws
MissingBaseUrlException
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
throws
CanvasApiException
throws
MissingApiKeyException
throws
MissingBaseUrlException
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
throws
CanvasApiException
throws
MissingApiKeyException
throws
MissingBaseUrlException
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
throws
CanvasApiException
throws
MissingApiKeyException
throws
MissingBaseUrlException
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
throws
CanvasApiException
throws
MissingApiKeyException
throws
MissingBaseUrlException
Return values
mixed

Decoded JSON response or raw response based on content type

isMultipartData()

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
bool

parseResponse()

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
example
// Sequential array
['colors' => ['red', 'blue']]
// Produces: colors[]=red, colors[]=blue

// Associative array
['user' => ['name' => 'John', 'age' => 30]]
// Produces: user[name]=John, user[age]=30

// Deeply nested
['appointment_group' => ['new_appointments' => [['2024-01-01'], ['2024-01-02']]]]
// Produces: appointment_group[new_appointments][0][]=2024-01-01,
//           appointment_group[new_appointments][1][]=2024-01-02
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


        
On this page

Search results