PHP Client SDK
The OneSignal PHP client is a server OneSignal SDK for PHP. Integrate OneSignal with your backend events, data, and more.
Backend SDKs in User Model Beta
You still can try using new User Model endpoints, we advise customers to not use these SDKs in production.
Requirements
PHP 7.3 and later.
Composer
Installation
Install the bindings via Composer, by adding the following to composer.json
:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/OneSignal/onesignal-php-api.git"
}
],
"require": {
"onesignal/onesignal-php-api": "*@dev"
}
}
Then run composer install
Usage examples
Imports
use DateTime;
use onesignal\client\api\DefaultApi;
use onesignal\client\Configuration;
use onesignal\client\model\GetNotificationRequestBody;
use onesignal\client\model\Notification;
use onesignal\client\model\StringMap;
use onesignal\client\model\Player;
use onesignal\client\model\UpdatePlayerTagsRequestBody;
use onesignal\client\model\ExportPlayersRequestBody;
use onesignal\client\model\Segment;
use onesignal\client\model\FilterExpressions;
use PHPUnit\Framework\TestCase;
use GuzzleHttp;
Constants
const APP_ID = '<YOUR_APP_ID>';
const APP_KEY_TOKEN = '<YOUR_API_KEY>';
const USER_KEY_TOKEN = '<YOUR_USER_KEY_TOKEN>';
Configure authorization
$config = Configuration::getDefaultConfiguration()
->setAppKeyToken(APP_KEY_TOKEN)
->setUserKeyToken(USER_KEY_TOKEN);
$apiInstance = new DefaultApi(
new GuzzleHttp\Client(),
$config
);
Notifications
Creating a notification model
function createNotification($enContent): Notification {
$content = new StringMap();
$content->setEn($enContent);
$notification = new Notification();
$notification->setAppId(APP_ID);
$notification->setContents($content);
$notification->setIncludedSegments(['Subscribed Users']);
return $notification;
}
Sending a notification immediately
$notification = createNotification('PHP Test notification');
$result = $apiInstance->createNotification($notification);
print_r($result);
Scheduling a notification to be sent in 24 hours
$notification = self::createNotification('PHP Test scheduled notification');
$dt = new DateTime();
$dt->modify('+1 day');
$notification->setSendAfter($dt);
$scheduledNotification = $apiInstance->createNotification($notification);
print_r($scheduledNotification);
Canceling a scheduled notification
$cancelResult = $apiInstance->cancelNotification(APP_ID, $scheduledNotification->getId());
print_r($cancelResult->getSuccess());
Retrieving a notification
$scheduledNotification = $apiInstance->getNotification(APP_ID, $scheduledNotification->getId());
print_r($scheduledNotification);
Listing notifications by application ID
$getResult = $apiInstance->getNotifications(APP_ID);
print_r($getResult->getNotifications());
Getting notification history
$requestBody = new GetNotificationRequestBody();
$requestBody
->setAppId(APP_ID)
->setEvents('sent');
$getResult = $apiInstance->getNotificationHistory($scheduledNotification->getId(), $requestBody);
print_r($getResult->getSuccess());
Players
Creating a new Player model
function createPlayerModel($playerId): Player {
$player = new Player();
$player->setAppId(APP_ID);
$player->setIdentifier($playerId);
$player->setDeviceType(1);
return $player;
}
Creating a Player
$player = createPlayerModel('php_test_player_id');
$createPlayerResult = $apiInstance->createPlayer($player);
print_r($createPlayerResult);
Getting a Player
$getPlayerResult = $apiInstance->getPlayer(APP_ID, 'php_test_player_id');
print_r($getPlayerResult);
Getting a list of Players
$limit = 10;
$getPlayersResult = $apiInstance->getPlayers(APP_ID, $limit);
print_r($getPlayersResult->getPlayers());
Deleting a player
$deletePlayerResult = $apiInstance->deletePlayer(APP_ID, 'php_test_player_id');
print_r($deletePlayerResult->getSuccess());
Exporting players into CSV-spreadsheet
$exportPlayersRequestBody = new ExportPlayersRequestBody();
$exportPlayersRequestBody->setExtraFields([]);
$exportPlayersRequestBody->setSegmentName('');
$exportPlayersResult = $apiInstance->exportPlayers(APP_ID, $exportPlayersRequestBody);
print_r($exportPlayersResult->getCsvFileUrl());
Segments
Creating a segment
// Settings up the filter. Filters determine a segment.
$filterExpressions = new FilterExpressions();
$filterExpressions->setField('session_count');
$filterExpressions->setRelation('>');
$filterExpressions->setValue('1');
Setting up the segment itself
$segment = new Segment();
$segment->setName('test_segment_name');
$segment->setFilters([$filterExpressions]);
$createSegmentResponse = $apiInstance->createSegments(APP_ID, $segment);
print_r($createSegmentResponse);
Deleting a segment
$deleteSegmentResponse = $apiInstance->deleteSegments(APP_ID, $createSegmentResponse->getId());
print_r($deleteSegmentResponse->getSuccess());
Working with Apps
Getting an app
$getAppResponse = $apiInstance->getApp(APP_ID);
print_r($getAppResponse);
Getting a list of apps
$getAppsResponse = $apiInstance->getApps();
print_r($getAppsResponse);
Updating an app
$getAppResponse = $apiInstance->getApp(APP_ID);
$getAppResponse->setName('php_test_app_name');
$updateAppResponse = $apiInstance->updateApp(APP_ID, $getAppResponse);
print_r($updateAppResponse);
Outcomes
$outcomeNames = "os__session_duration.count,os__click.count";
$outcomeTimeRange = "1d";
$outcomePlatforms = "5";
$outcomeAttribution = "direct";
$outcomesResponse = $apiInstance->getOutcomes(APP_ID, $outcomeNames, null, $outcomeTimeRange, $outcomePlatforms, $outcomeAttribution);
print_r($outcomesResponse->getOutcomes());
API Endpoints
All URIs are relative to https://onesignal.com/api/v1
Class | Method | HTTP request | Description |
---|---|---|---|
DefaultApi | cancelNotification | DELETE /notifications/{notification_id} | Stop a scheduled or currently outgoing notification |
DefaultApi | createApp | POST /apps | Create an app |
DefaultApi | createNotification | POST /notifications | Create notification |
DefaultApi | createPlayer | POST /players | Add a device |
DefaultApi | createSegments | POST /apps/{app_id}/segments | Create Segments |
DefaultApi | deletePlayer | DELETE /players/{player_id} | Delete a user record |
DefaultApi | deleteSegments | DELETE /apps/{app_id}/segments/{segment_id} | Delete Segments |
DefaultApi | exportPlayers | POST /players/csv_export?app_id={app_id} | CSV export |
DefaultApi | getApp | GET /apps/{app_id} | View an app |
DefaultApi | getApps | GET /apps | View apps |
DefaultApi | getNotification | GET /notifications/{notification_id} | View notification |
DefaultApi | getNotificationHistory | POST /notifications/{notification_id}/history | Notification History |
DefaultApi | getNotifications | GET /notifications | View notifications |
DefaultApi | getOutcomes | GET /apps/{app_id}/outcomes | View Outcomes |
DefaultApi | getPlayer | GET /players/{player_id} | View device |
DefaultApi | getPlayers | GET /players | View devices |
DefaultApi | updateApp | PUT /apps/{app_id} | Update an app |
DefaultApi | updatePlayer | PUT /players/{player_id} | Edit device |
DefaultApi | updatePlayerTags | PUT /apps/{app_id}/users/{external_user_id} | Edit tags with external user id |
Models
- App
- BasicNotification
- BasicNotificationAllOf
- BasicNotificationAllOfAndroidBackgroundLayout
- Button
- CancelNotificationSuccessResponse
- CreateNotificationBadRequestResponse
- CreateNotificationSuccessResponse
- CreatePlayerSuccessResponse
- CreateSegmentBadRequestResponse
- CreateSegmentConflictResponse
- CreateSegmentSuccessResponse
- DeletePlayerBadRequestResponse
- DeletePlayerNotFoundResponse
- DeletePlayerSuccessResponse
- DeleteSegmentBadRequestResponse
- DeleteSegmentNotFoundResponse
- DeleteSegmentSuccessResponse
- DeliveryData
- ExportPlayersRequestBody
- ExportPlayersSuccessResponse
- Filter
- FilterExpressions
- FilterNotificationTarget
- GetNotificationRequestBody
- InvalidIdentifierError
- Notification
- Notification200Errors
- NotificationAllOf
- NotificationHistoryBadRequestResponse
- NotificationHistorySuccessResponse
- NotificationSlice
- NotificationTarget
- NotificationWithMeta
- NotificationWithMetaAllOf
- Operator
- OutcomeData
- OutcomesData
- PlatformDeliveryData
- PlatformDeliveryDataEmailAllOf
- PlatformDeliveryDataSmsAllOf
- Player
- PlayerNotificationTarget
- PlayerSlice
- Purchase
- Segment
- SegmentNotificationTarget
- StringMap
- UpdatePlayerSuccessResponse
- UpdatePlayerTagsRequestBody
- UpdatePlayerTagsSuccessResponse
Authorization
All the OneSignal endpoints require either an app_key or user_key tokens for authorization. It is recommended to set up both of those keys during the initial config initialization so that you don't need to worry about which endpoint requires app_key and which user_key. You can get the value of these keys from your
app dashboard and user settings pages.
Updated 3 months ago