A powerful way to send personalized messages at scale and build effective customer engagement strategies. Learn more at onesignal.com
For more information, please visit https://onesignal.com.
PHP 7.3 and later.
To install the bindings via Composer, add 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
Download the files and include autoload.php
:
<?php
require_once('/path/to/OneSignal/vendor/autoload.php');
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;
const APP_ID = '<YOUR_APP_ID>';
const APP_KEY_TOKEN = '<YOUR_APP_KEY_TOKEN>';
const USER_KEY_TOKEN = '<YOUR_USER_KEY_TOKEN>';
$config = Configuration::getDefaultConfiguration()
->setAppKeyToken(APP_KEY_TOKEN)
->setUserKeyToken(USER_KEY_TOKEN);
$apiInstance = new DefaultApi(
new GuzzleHttp\Client(),
$config
);
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;
}
$notification = createNotification('PHP Test notification');
$result = $apiInstance->createNotification($notification);
print_r($result);
$notification = self::createNotification('PHP Test scheduled notification');
$dt = new DateTime();
$dt->modify('+1 day');
$notification->setSendAfter($dt);
$scheduledNotification = $apiInstance->createNotification($notification);
print_r($scheduledNotification);
$cancelResult = $apiInstance->cancelNotification(APP_ID, $scheduledNotification->getId());
print_r($cancelResult->getSuccess());
$scheduledNotification = $apiInstance->getNotification(APP_ID, $scheduledNotification->getId());
print_r($scheduledNotification);
$getResult = $apiInstance->getNotifications(APP_ID);
print_r($getResult->getNotifications());
$requestBody = new GetNotificationRequestBody();
$requestBody
->setAppId(APP_ID)
->setEvents('sent');
$getResult = $apiInstance->getNotificationHistory($scheduledNotification->getId(), $requestBody);
print_r($getResult->getSuccess());
function createPlayerModel($playerId): Player {
$player = new Player();
$player->setAppId(APP_ID);
$player->setIdentifier($playerId);
$player->setDeviceType(1);
return $player;
}
$player = createPlayerModel('php_test_player_id');
$createPlayerResult = $apiInstance->createPlayer($player);
print_r($createPlayerResult);
$getPlayerResult = $apiInstance->getPlayer(APP_ID, 'php_test_player_id');
print_r($getPlayerResult);
$limit = 10;
$getPlayersResult = $apiInstance->getPlayers(APP_ID, $limit);
print_r($getPlayersResult->getPlayers());
$deletePlayerResult = $apiInstance->deletePlayer(APP_ID, 'php_test_player_id');
print_r($deletePlayerResult->getSuccess());
$exportPlayersRequestBody = new ExportPlayersRequestBody();
$exportPlayersRequestBody->setExtraFields([]);
$exportPlayersRequestBody->setSegmentName('');
$exportPlayersResult = $apiInstance->exportPlayers(APP_ID, $exportPlayersRequestBody);
print_r($exportPlayersResult->getCsvFileUrl());
// Settings up the filter. Filters determine a segment.
$filterExpressions = new FilterExpressions();
$filterExpressions->setField('session_count');
$filterExpressions->setRelation('>');
$filterExpressions->setValue('1');
$segment = new Segment();
$segment->setName('test_segment_name');
$segment->setFilters([$filterExpressions]);
$createSegmentResponse = $apiInstance->createSegments(APP_ID, $segment);
print_r($createSegmentResponse);
$deleteSegmentResponse = $apiInstance->deleteSegments(APP_ID, $createSegmentResponse->getId());
print_r($deleteSegmentResponse->getSuccess());
$getAppResponse = $apiInstance->getApp(APP_ID);
print_r($getAppResponse);
$getAppsResponse = $apiInstance->getApps();
print_r($getAppsResponse);
$getAppResponse = $apiInstance->getApp(APP_ID);
$getAppResponse->setName('php_test_app_name');
$updateAppResponse = $apiInstance->updateApp(APP_ID, $getAppResponse);
print_r($updateAppResponse);
$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());
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 |
- 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
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.
- Type: Bearer authentication
- Type: Bearer authentication
- API version:
1.0.1
- Package version:
1.0.0
- Package version: