Send Data with PHP

For those of you that use PHP, here is a quick script you can use to send data to your documents and data routes.

$data = array(
    'OrgName' => 'Org 1', 
    'OrgAddress' => 'Address 1', 
    'OrgCity' => 'Orlando', 
    'OrgState' => 'FL', 
    'OrgZip' => '32819'
);

$json_data = json_encode($data); 

$url = 'https://www.webmerge.me/merge/10690/me1ket';

//open connection
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($json_data))
);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

if ($result) {
    $response = 'Document created successfully. Check WebMerge for more details.';
} else {
    $response = 'No documents created.';
}

echo $response;

 

Combine PDF Documents

If you'd like to send all your data for multiple merges (of the same document) at once and receive back a "combined" document with all of the individual merges as separate pages, you just need to make one minor adjustment to the code above

$data = array(
    '_combine' => array(
        array(
            'OrgName' => 'Org 1', 
            'OrgAddress' => 'Address 1', 
            'OrgCity' => 'Orlando', 
            'OrgState' => 'FL', 
            'OrgZip' => '32819'
        ),
        array(
            'OrgName' => 'Org 2', 
            'OrgAddress' => 'Address 2', 
            'OrgCity' => 'Orlando', 
            'OrgState' => 'FL', 
            'OrgZip' => '32819'
        ),
    )
);

Notice the individual data sets are wrapped in an array that has a key "_combined". This tells the merge system to create a document with each array inside the _combined key and then create a single PDF

Have more questions? Submit a request

2 Comments

  • Avatar
    Alex Sirota

    This works really great especially for testing from a PHP harness that calls another API (we're using Wild Apricot). Is there a way to redirect to the completed PDF on screen rather than just echo a success/fail response?

  • Avatar
    Jeremy Clarke

    Hi Alex,

    Yes, you can add a "download=1" parameter to your Merge URL and we'll return the document in the response (instead of JSON), then you can display the file to the user like this:

    header('Content-Description: File Transfer');
    header("Content-Type: application/octet-stream");
    header('Content-Disposition: attachment; filename="The File.pdf";');
    header("Content-Length: ".strlen($response));
    print $response;

Please sign in to leave a comment.
Powered by Zendesk