1. Go to this page and download the library: Download onesignal/onesignal-php-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
onesignal / onesignal-php-api example snippets
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 REST_API_KEY_TOKEN = '<YOUR_REST_API_KEY>'; // App REST API key d for creating new apps and other top-level endpoints
$config = Configuration::getDefaultConfiguration()
->setRestApiKeyToken(REST_API_KEY_TOKEN)
->setOrganizationApiKeyToken(ORGANIZATION_API_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);
$notification = createNotification('PHP Test filtered notification');
$filter1 = new Filter();
$filter1->setField('amount_spent');
$filter1->setRelation('=');
$filter1->setValue('0');
$notification->setFilters([$filter1]);
$result = $apiInstance->createNotification($notification);
print_r($result);
$notification = createNotification('PHP Test notification');
$result = $apiInstance->createNotification($notification);
print_r($result);
function createPlayerModel($playerId): Player {
$player = new Player();
$player->setAppId(APP_ID);
$player->setIdentifier($playerId);
$player->setDeviceType(1);
return $player;
}
// Settings up the filter. Filters determine a segment.
$filterExpressions = new FilterExpressions();
$filterExpressions->setField('session_count');
$filterExpressions->setRelation('>');
$filterExpressions->setValue('1');