Tutorials

Download

Adding Contacts through the API

Need contacts for your email marketing campaigns? Osmek allows you to add new contacts directly from your site through the API. This tutorial shows how.

Before we can add a new contact, we need set up a form to get their information.

index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>Add a Contact</title>
    </head>
    <body>
        
        <h1>Add a Contact</h1>
        
        <form id="contact" action="contact.php" method="post">
            <label for="first_name">First Name</label>
            <input type="text" name="first_name" id="first_name" />
            <label for="last_name">Last Name</label>
            <input type="text" name="last_name" id="last_name" />
            <label for="contact_email">Email</label>
            <input type="text" name="contact_email" id="contact_email" />
            <button type="submit">Submit</button>
        </form>

    </body>
</html>

To add a new contact we use Osmek.php's new_contact function. This function takes two parameters: the section id and an array of contact info.

contact.php

<?php
    
// Include Osmek and instatiate the Osmek class providing your api key
    
include('Osmek.php');
    
$osmek = new Osmek('[api_key]');
    
    
$contact = array(
        
'first_name' => $_POST['first_name'],
        
'last_name' => $_POST['last_name'],
        
'email' => $_POST['contact_email']
    );
    
// Pass the contact array and section id to $osmek->new_contact.  Osmek will respond with json
    // Note: You must turn the subscriber option on in your section's settings
    
$response $osmek->new_contact([section_id], $contact);
    
$response json_decode($response);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>Add a Contact</title>
    </head>
    <body>
        <!-- Echo Response status and message -->
        <h2 class="alert"><?=$response->status?><?=$response->msg?></h2>
    </body>
</html>