Post to Friendly via the API (PHP)

In this example we will crate a new contact and tag them. This is a great way to start a campaign for them.

Let’s declare our variables:

<?php
$loginname = 'apiuser';
// Loginname of your API user
$password = 'yourpasswordhere';
// This is the password of the API user
$siteurl = 'friendlysubdomain.yoursiteurl.tld';
// example: mymautic.com

The data you would like to transfer is called payload. We will set it for now, and later replace with the data we get from the webhook.

$email = '[email protected]';
$firstname = 'Jean-Luc';
$lastname = 'Pickard';
$tag = 'purchased';

This is our curl call:

$curl = curl_init();
// Set some options - we are passing in a user agent too here
  curl_setopt_array($curl, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL => "https://".$loginname.":".$password."@".$siteurl."/api/contacts/new",
  CURLOPT_USERAGENT => 'Friendly API Call',
  CURLOPT_POST => 1,
// posting the payload
  CURLOPT_POSTFIELDS => array(
    'firstname' => $firstname,
    'lastname' => $lastname,
    'email' => $email,
    'tags' => $tag
  )
));
curl_exec($curl);

Save the code in a php file, place it in your server, and run it.

Our new contact will be placed into Fraiendly tagged with the appropriate tag. If the email address already existed, the contact will be updated.


Do you have any questions? We are happy to help you via 📧 email, ☎️ phone and 👩‍💻 video calls.

Click here to go back to the main help page:

Friendly Automate