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_APP_KEY_TOKEN>';
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

ClassMethodHTTP requestDescription
DefaultApicancelNotificationDELETE /notifications/{notification_id}Stop a scheduled or currently outgoing notification
DefaultApicreateAppPOST /appsCreate an app
DefaultApicreateNotificationPOST /notificationsCreate notification
DefaultApicreatePlayerPOST /playersAdd a device
DefaultApicreateSegmentsPOST /apps/{app_id}/segmentsCreate Segments
DefaultApideletePlayerDELETE /players/{player_id}Delete a user record
DefaultApideleteSegmentsDELETE /apps/{app_id}/segments/{segment_id}Delete Segments
DefaultApiexportPlayersPOST /players/csv_export?app_id={app_id}CSV export
DefaultApigetAppGET /apps/{app_id}View an app
DefaultApigetAppsGET /appsView apps
DefaultApigetNotificationGET /notifications/{notification_id}View notification
DefaultApigetNotificationHistoryPOST /notifications/{notification_id}/historyNotification History
DefaultApigetNotificationsGET /notificationsView notifications
DefaultApigetOutcomesGET /apps/{app_id}/outcomesView Outcomes
DefaultApigetPlayerGET /players/{player_id}View device
DefaultApigetPlayersGET /playersView devices
DefaultApiupdateAppPUT /apps/{app_id}Update an app
DefaultApiupdatePlayerPUT /players/{player_id}Edit device
DefaultApiupdatePlayerTagsPUT /apps/{app_id}/users/{external_user_id}Edit tags with external user id

Models

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.