Zapier is great if you want to create a proof of concept automation or test your workflows. However if you want to scale up your automation, Zapier can be super-expensive. And all it does is take data from one place and puts it into another place. If you use Zapier for couple of simple repetitive jobs, its better to cut out the middleman and use your own code.
This is what we gonna do
We will create few line of code, that captures the incoming data from a webhook, process it if needed, and post it to our Mautic using simple authentication.
Understand the basics
You don’t need to be a programmer to do this, with the help of google and Stackoverflow you can write the script, that replaces Zapier.
Here are the key expressions you need to know. You need to know what you are using in order to research more, or if you don’t want to look stupid if you ask questions from others in the forums.
Here we go:
A webhook is like a package of information sent to your script over the internet. It can be different format or structure. The good thing, is that you know what to expect, when you write your code to receive it. Check the other party’s website to understand what form of data to expect.
Basic auth is the form of authentication we use to talk to our Mautic instance. It is super simple, just like a login, only with a line of code.
A Curl call is similar to a webhook. You can use it to post the data you to your Mautic.
API stands for Application Programming Interface, and in our case it is the part, that accepts our package and places the data in the right place.
All Zapier does is accept the webhook, and use basic auth to reach your Mautic via curl formatted according to the API docs.
Not rocket science.
Based on comments I would like to lay out the plan, what we intent to do.
We’d like to create a connector file and save it in our WordPress, or our other system, that we use with Mautic. Not in our Mautic install. This can be any folder, that you can reach from the internet.
We can save a file in our Mautic’s install as well, but have to be careful, as Mautic will restrict filenames you can use. If you make an index.php, than you are fine, anything else will break.
So you could make a /connector/index.php file in your mautic subfolder and you would be set. Your system looks like this then:
Okay, before we get started, let’s turn on the API settings in Mautic. I’d create a new user called API as well, just to keep the simple login and programmatic login separate.
Post to Mautic via the API
What? Why to start from the last step?
Multiple reasons. I want to make sure the API works, and I also want to you to have instant success.
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 = 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@example.com'; $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 => 'Mautic Connector', CURLOPT_POST => 1, // posting the payload CURLOPT_POSTFIELDS => array( 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $email, 'tags' => $tag ) ));
Save the code in a php file, place it in your server (outside of your Mautic folders), and run it.
Our new contact will be placed into Mautic tagged with the appropriate tag.
Capture and process a webhook (woocommerce example)
Now I have a bad news and good news. The bad news is, that each service you want to connect with might have a different webhook structure. The good news is, that we need to build the interface only once. With a little practice and the help of the internet you’ll be able to catch any webhook payload in no time.
In our example we will capture a woocommerce webhook.
As a first step, you can should see how the payload looks like. You can do it in 3 easy steps:
- create a bin at https://requestbin.com/
- add the bin address as your webhook address
- Fire up the webhook. In our case that would be making a woocommerce order
If all goes fine, you’ll be able to see an incoming webhook payload in your bin.
We can capture the entire webhook’s payload with just one command.
// receive the webhook $HTTP_RAW_POST_DATA = file_get_contents('php://input');
The hard part is to make sense of this incoming data. Teaching you how to decode and loop through JSON content is beyond the scope of this tutorial, but I’ll show you how I did it with couple of commands in case of this woocommerce webhook:
// decode the incoming data $data = json_decode($HTTP_RAW_POST_DATA,true);
Now we can grab the different values. In case of woocommerce we will use the billing information:
$email = $data['billing']['email']; $firstname = $data['billing']['first_name']; $lastname = $data['billing']['last_name'];
Obviously this info comes to the front of our script, and replaces the payload we declared before. The tag is static, you don’t need to replace it. Maybe I’ll create a second part to this article where we tag our contact based on what they have purchased.
I am huge fan of logging, so why don’t you add a small logfile to your script. It makes debugging easier. Add this at the end of your code:
If you have questions, let me know in the comments.