Before making any API requests, you must initialize the HttpClient using the init() method. This method sets up the global Connection object required for all subsequent requests.
To comply with Unsplash API terms, you must provide a utmSource in your credentials array. If it is missing, a PHP error will be triggered.
Credentials Array
Required keys for authentication:
applicationId (string): Your Unsplash Application ID.secret (string): Your Application Secret (required for OAuth).callbackUrl (string): The URL to redirect to after OAuth authentication.utmSource (string): The name of your application (required for API terms).
Access Token Array
If you already have an access token (e.g., from a previous session), you can pass it to init() to avoid re-authenticating:
access_token (string): The identifier.refresh_token (string): Necessary when the access token expires.expires_in (int): The expiration duration.
use Unsplash\HttpClient;
// Initialize with credentials
HttpClient::init([
'applicationId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'callbackUrl' => 'https://your-app.com/callback',
'utmSource' => 'my_awesome_app'
]);
// Or initialize with an existing access token
HttpClient::init([
'applicationId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'callbackUrl' => 'https://your-app.com/callback',
'utmSource' => 'my_awesome_app'
], [
'access_token' => 'EXISTING_ACCESS_TOKEN',
'refresh_token' => 'EXISTING_REFRESH_TOKEN',
'expires_in' => 3600
]);