• In this tutorial, I'll show you how to post form data using Zapier's Webhooks. 

    Integrating simple html forms with Zapier has proven useful for me.

    As an example, we'll create an email newsletter optin form that collects email addresses and pushes them to Zapier via cURL and then adds them to a Google Sheet.

    Posting JSON to Zapier

    Let's start with the cURL request and then later consider how it interfaces with Zapier.

    What is cURL?

    Put simply, cURL is just a library that lets you make HTTP requests in PHP.

    PHP supports libcurl, a library that allows you to connect and communicate to servers with various protocols. libcurl currently supports:

    • http
    • https
    • ftp
    • gopher, telnet, dict, file, ldap protocols, and others

    You can check if cURL is enabled on your server by checking your info.php. If you don't have cURL, it's easy to install on linux via the command line:

    $ sudo apt-get install curl
    $ sudo service apache2 restart
    $ sudo apt-get install php5-curl
    $ sudo service apache2 restart

    Here's what our HTTP request to Zapier looks like:

    <?php
    
    function zapier($url, $json, $headers) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($ch);
        echo $output;
        curl_close($ch);
    }
    
    $url = ''; // add your Zapier webhook url 
    $json = json_encode($_POST);
    $headers = array('Accept: application/json', 'Content-Type: application/json');
    // call the zapier() function
    zapier($url, $json, $headers); ?>
    

    Here's the cURL request to Zapier. The $url variable below is just the endpoint Zapier provides when you setup the webook for the first time (more on this later). Formally, $url will be set to something like https://hooks.zapier.com/hooks/catch/11111/xoxoxo/

    For the purpose of this example, let's drop this code is in a file called post.php.

    The zapier() function that takes the $url, $json, and $headers as arguments.

    If you want to quickly prove to yourself that the cURL request works, follow these quick steps:

    1. Change $json = json_encode($_POST); to $json = json_encode( array( "animal" => "turtle" ) );
    2. Get your webhook url from Zapier (I'll show you how later), and set it to $url
    3. Save the file locally on your computer.

    Finally, open the command line, cd into the folder where you've copied post.php, and execute the php script with:

    php /your-path/post.php

    You should see an output in the command line that looks like:

    {"status": "success", "attempt": "320eab3z-103b-430c-a78f-f84a8a0z30f0", "iz": "f767lfv8-1495-4616-a67c-604z3z396bcb", "request_iz": "hfBza0sLiPLu8AlM"}

    (some values changed to protect the innocent).

    Using cURL to Pass Form Data to Zapier

    Step 1

    Create a simple form as follows and point it to the <code>post.php</code> you just made:

    <form method="post" action="/path/to/post.php">
    <input type="email" name="email">
    <input type="submit" value="Submit">
    </form>

    You can also remove the action attribute (<code>action="http://..."</code>) and just post the form to itself (and add your curl script at the top of the page).

    When you post the form, it will send over the <code>$_POST</code> array:

    $_POST = array("email" => "name@example.com");

    which with the php function json_encode returns the JSON representation of our array:

    {"email":"name@example.com"}

    Lastly, cURL makes the HTTP request to Zapier delivering that JSON payload.

    The Complete Working Example

    Go to the github repo for this project or type git clone https://github.com/unshift/zapier-webhook-example in terminal.​ 

    Setting up Zapier

    Step One

    Create a new zap. Use "Webooks by Zapier."

    Step Two

    Select Catch Hook.

    Copy your custom webhook URL and paste it in like so:

    $url = 'https://hooks.zapier.com/hooks/catch/10532156/d6ba34/';