Forum for discussing general topics related to Couch.
2 posts Page 1 of 1
Hello!

Has anyone here ever made Couch work with Zapier?
There's ways to make work pretty much anything with Zapier, but I'm not used to work with these kind of custom webhooks.

I know there's no functions out of the box for this, but would it be possible?
If so, any idea how I could get started?

One thing I have found on their website is:

Code: Select all
Sending more than one event per webhook request
Webhook triggers can receive more than one event in a single webhook request. You can send an array of properly formed JSON objects, and Zapier will trigger the Zap once for each object in the array.

For example, if you POST this payload to a Webhook endpoint:

[
{
    "first_name": "Bryan",
    "last_name": "Helmig",
    "age": 27
},
{
    "first_name": "Mike",
    "last_name": "Knoop",
    "age": 28
},
{
    "first_name": "Wade",
    "last_name": "Foster",
    "age": 29
}
]
Zapier will trigger the actions three times, once for every object in the array.


I'm pretty sure it would be possible to get the data from a clonable page and turn it into JSON, right?

Thanks!
Hi,

There are two points to address in your use-case -
1. Post a JSON object to the webhook url (zapier in this case)
2. Create the JSON object used above from Couch pages/templates.

As for the first point, we can use the 'curl' library (usually available on all systems) with some PHP to post the data.
To make it a tad easier, I have whipped up a quick-n-dirty tag named <cms:zapier> which you can get by pasting the code below in your couch/addons/kfunctions.php file -
Code: Select all
$FUNCS->register_tag( 'zapier', function($params, $node){
    global $CTX, $FUNCS;
    extract( $FUNCS->get_named_vars(
                array(
                        'url'=>'',
                        'json'=>'',
                        ),
                $params)
            );

    if( !extension_loaded('curl') ){ die( "Error in tag '".$node->name."': curl extension not available" ); }
    $url = trim( $url );
    if( !strlen($url) ) die( "Error in tag '".$node->name."': url empty" );
    $headers = array( 'Accept: application/json', 'Content-Type: application/json' );

    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_TIMEOUT, 20 );
    curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
    curl_setopt( $ch, CURLOPT_POST, 1 );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $json );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
    $html = curl_exec($ch);

    curl_close( $ch );

    return $html;
});

Now assuming variables named 'my_webhook_url' and 'my_json' are available at a point in your template containing the webhook url and the json payload respectively, here is how you can use the new tag -
Code: Select all
<cms:zapier my_webhook_url my_json />

Ok, so that is point one addressed.
Coming to the second point, how to get a valid JSON object from Couch pages?
If the data is coming from a repeatable-region, it could be as easy as follows -
Code: Select all
<cms:set my_json="<cms:show_repeatable 'my_repeatable' as_json='1' />" />

For discrete data spread over several regions, please see the following -
viewtopic.php?f=5&t=10892

Hope this helps.

P.S. You might find https://requestbin.com/ a useful resource to create a webhook for posting test data and verifying everything is as expected.
2 posts Page 1 of 1
cron