PHP code example of capirussa / pushover-php

1. Go to this page and download the library: Download capirussa/pushover-php 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/ */

    

capirussa / pushover-php example snippets


use Capirussa\Pushover;

try {
    $client = Pushover\Client::init($applicationToken);
    $message = new Pushover\Message('userKey', 'message');

    $client->send($message);
} catch (Pushover\Exception $exception) {
    // something went wrong, fix it and try again!
}

$message = new Pushover\Message();
$message->setRecipient('userKey');
$message->setMessage('message');
$message->setSound(Pushover\Sound::SPACE_ALARM);

$receipt = new Pushover\Receipt();
$receipt->setReceipt('receiptToken');

$validate = new Pushover\Validate();
$validate->setRecipient('userToken');
$validate->setDevice('iphone');

use Capirussa\Pushover;

// initialize the client
$client = new Pushover\Client('123apptoken');

// optional: when running on an ill-configured development server
if ($sslIsBroken) {
    $client->disableSslVerification();
}

// let's verify whether the user token is valid
$validateRequest = new Pushover\Validate('usertoken123');
if (!$client->validate($validateRequest)) {
    echo 'Hey, "usertoken123" is not a valid token!';
} else {
    // let's send a message with regular priority
    $messageRequest = new Pushover\Message('usertoken123');
    $messageRequest->setTitle('Normal priority message');
    $messageRequest->setMessage('This is just a regular message.');

    $client->send($messageRequest);

    // now let's get a list of all devices for this user
    $devices = $client->getUserDevices('usertoken123');

    // now let's send a message with emergency priority to the user's first device
    $emergency = new Pushover\Message('usertoken123');
    $emergency->setDevice($devices[0]);
    $emergency->setPriority(Pushover\Request::PRIORITY_EMERGENCY);
    $emergency->setSound(Pushover\Sound::SIREN);
    $emergency->setTitle('Everything is broken');
    $emergency->setMessage('You must fix it, now!');
    $emergency->setCallbackUrl('http://example.com/fixitnow');

    $receiptToken = $client->send($emergency);

    // now let's see whether the user has acknowledged the message
    $receiptRequest = new Pushover\Receipt($receiptToken);

    $response = $client->pollReceipt($receiptRequest);

    // check whether that request was successful
    if ($response->getStatus() === Pushover\Response::STATUS_SUCCESS) {
        // check whether the request was acknowledged
        $acknowledged = ($response->getAcknowledged() === Pushover\Response::ACKNOWLEDGED_YES);
        if ($acknowledged) {
            // get the user token of the user who acknowledged the request
            $userToken = $response->getAcknowledgedBy();

            // get the DateTime at which the token was acknowledged
            $when = $response->getAcknowledgedAt();

            echo 'The emergency was acknowledged by ' . $userToken . ' on ' . $when->format('Y-m-d') . ' at ' . $when->format('H:i:s');

            // get whether the callback URL was called
            $calledBack = ($response->getCalledBack() === Pushover\Response::CALLED_BACK_YES);
            if ($calledBack) {
                echo 'The callback URL was requested by Pushover on ' . $response->getCalledBackAt()->format('Y-m-d') . ' at ' . $response->getCalledBackAt()->format('H:i:s');
            }
        } else {
            // check whether the message has expired
            $expired = ($response->getExpired() == Pushover\Response::EXPIRED_YES);

            if ($expired) {
                echo 'The message has not been acknowledged, and has expired. Tough cookie.';
            } else {
                echo 'The message has not been acknowledged yet, it was last sent to the user at ' . $response->getLastDeliveredAt()->format('H:i:s') . ' and will expire on ' . $response->getExpiresAt()->format('Y-m-d') . ' at ' . $response->getExpiresAt()->format('H:i:s');
            }
        }
    }
}