Categories
tracking

How to create a webhook to send lead from Unbounce to Pardot in PHP

When you work with Unbounce to create landing page and want to send lead to Salesforce. you can think that Zapier is the solution. But it’s not. For a lot of reason, the settings of such a zap is always a little nightmare.

After some try, you will finally ending by trying something else, like the webhook option.

The webhook let you do exactly do want you want to achieve, nothing less, nothing more.

Tutorial

  • Go to your unbounce page / Integration / Webhook : create a new webhook
  • Set the url on the first field : “https://my-domain/webhook/unbounce.php”
  • in this example, I choose to work with JSON, so I select this option
  • I choose to add a custom header to be sure than the url of the webhook will only be used by Unbounce, I create a secret key with the name “X-Custom-Secret” and the value “your-secret-key”.
  • then, you can see what is inside unbounce form data : the label of the field must be used in your webhook
  • Also you can add custom field if needed.
  • Save the setting

Code of the webhook file

You will place it here, for example : https://my-domain/webhook/unbounce.php

<?php

$secretkey = 'your-secret-key';

header('Content-Type: application/json');

// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	
	
	$receivedSecretKey = trim($_SERVER['HTTP_X_CUSTOM_SECRET']);

    // Validate the secret key
    if ($receivedSecretKey !== $secretkey) {
        http_response_code(403); // Forbidden
        echo json_encode(['error' => 'Forbidden: Invalid secret key']);
        exit;
    }
	
    // Read the incoming request
    $contentType = $_SERVER['CONTENT_TYPE'];
    $rawData = file_get_contents('php://input');
    
    if (strpos($contentType, 'application/json') !== false) {
        $data = json_decode($rawData, true);
    } else {
        http_response_code(400);
        echo json_encode(['error' => 'Unsupported Content Type']);
        exit;
    }
    
    // Log the incoming data for debugging
    file_put_contents('unbounce_webhook.log', print_r($data, true), FILE_APPEND);
    
    // Process the data as needed
    // For example, extract some fields
    $email = $data['email'];
     
	
	
    $fields = array(
		"email" => $email
	);


	$fields_string = http_build_query($fields);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'url of your pardot form handler');
	curl_setopt($ch, CURLOPT_POST,1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$response = curl_exec($ch);
	
    
    
    // Respond back to Unbounce
    echo json_encode(['status' => 'success', 'message' => 'Data received']);
} else {
    http_response_code(405);
    echo json_encode(['error' => 'Method Not Allowed']);
}

Leave a Reply