Tutorials
DownloadAdding Subscribers through the API
Want to notify your readers of new content? Osmek can automatically send your subscribers an email notification when you save a new post. This tutorial shows how to add new subscribers through the api.
Before we can add a new subscriber, we need set up a form to get an email address. Let's post this form to subscribe.php.
index.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Subscribe</title>
</head>
<body>
<h1>Subscribe</h1>
<form id="subscribe" action="subscribe.php" method="post">
<input type="text" name="subscriber_email" id="subscriber_email" />
<button type="submit">Submit</button>
</form>
</body>
</html>
To add a new subscriber we use Osmek.php's subscribe function. This function takes two parameters: an email address and the section id.
subscribe.php
<?php
// Include Osmek and instatiate the Osmek class providing your api key
include('Osmek.php');
$osmek = new Osmek('[api_key]');
// Pass the email address and section id to $osmek->subscribe. Osmek will respond with json
// Note: You must turn the subscriber option on in your section's settings
$response = $osmek->subscribe($_POST['subscriber_email'], [section_id]);
$response = json_decode($response);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Subscribe</title>
</head>
<body>
<!-- Echo Response status and message -->
<h2 class="alert"><?=$response->status?>, <?=$response->msg?></h2>
</body>
</html>