Subscriptions and Cronjobs in PHP

Mike Stowe
RingCentral Developers

--

In the first API Spotlight we talked about how to create a simple SMS keyword app.

The key API behind this functionality of course is the Subscription API, which notifies our script that a new text/ SMS message was received and then lets the app logic decide what to do with it.

While subscriptions are extremely powerful and useful — they can also be a little bit tricky, and a little dangerous. One of the risks with a subscription is if a malicious user hacks your account and sets up their own subscription. Of course, you should be monitoring what subscriptions are setup using the /subscription resource, but to add an extra layer of protection any subscriptions created automatically expire after 7 days.

This means that after you setup your subscription, you’ll need to setup a cronjob to renew the subscription within that time period. The good news is that this process is pretty straight forward and easy.

Before we continue, this tutorial assumes you already have the PHP SDK installed and have the password flow available for your app. If you don’t (and that’s ok) or you’re not sure, jump back to the SMS Tutorial to learn how to setup your app and install the SDK.

First we just need to write a quick PHP script that renews our subscription:

#!/usr/bin/php
<?php
// Include Libraries
require('vendor/autoload.php');
// Setup Client
$sdk = new RingCentral\SDK\SDK(CLIENT_ID, CLIENT_SECRET, RingCentral\SDK\SDK::SERVER_PRODUCTION);
if (!$sdk->platform()->loggedIn()) {
$sdk->platform()->login(username, extension, password);
}
// Send SMS Message
$apiResponse = $sdk->platform()->post('/subscription/{SUBSCRIPTION_ID}/renew');
?>

Tip: If you don’t know the subscription ID, you can find it by calling the /subscription resource

Now that we have the script created, the next step is to make sure it has executable permissions. To do this, we’ll run the following chmod command:

chmod +x /path/to/file.php

Finally , we can create our cronjob. To open the Crontab editor we’ll run the following command in our terminal:

crontab -e

Depending on if you already have other cronjobs setup, this will either open a blank editor, or have something that looks like this

Since we’re adding a new cronjob, we’ll want to jump to the end of the file (assuming there’s something already there). To do this type the following:

G$

If you notice it writing the text at the top of the screen, delete the text you just entered and press escape and try again.

Now press “i” to enter edit mode. If you aren’t on a new line, click return and then type the following:

0 0 * * MON,THU /full/path/to/your/script.php

Now hit escape and type the following and press enter:

wq!

This will write and quit the file. You should see your terminal say something along the lines of “creating the new cronjob”.

And that’s it. You now have a cronjob to renew your subscription every Monday and Thursday (twice a week just to be safe).

--

--

Mike Stowe
RingCentral Developers

Developer, actor, and a *really* bad singer. Fan of APIs, Microservices, and #K8s.