· 6 years ago · Feb 15, 2020, 03:02 PM
1<?php
2/**
3 * da-api.php
4 *
5 * Connector to Docassemble API
6 *
7 * @author Alex Clark
8 * @copyright 2020, Metatheria, LLC
9 * @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU Public License version 2
10 */
11
12ini_set('display_errors', 1);
13ini_set('display_startup_errors', 1);
14error_reporting(E_ALL);
15
16define('PL_DISABLE_SECURITY', true);
17
18chdir('../');
19require_once('pika-danio.php');
20pika_init();
21
22$docassemble_url = pl_settings_get('docassemble_url');
23$docassemble_key = pl_settings_get('docassemble_key');
24$docassemble_interview = pl_grab_get('i');
25
26
27if (!(strlen($docassemble_url) > 0 & strlen($docassemble_key) > 0)){
28 die('Docassemble base url and api key must be set from the system settings screen');
29}
30
31if (!(strlen($docassemble_interview) > 0)) {
32 die('No interview parameter specified');
33}
34
35
36// Set up a curl to get a session id and secret for interaction with a Docassemble interview
37$ch = curl_init();
38
39curl_setopt($ch, CURLOPT_URL, $docassemble_url . '/api/session/new?i=' . $docassemble_interview . '&key=' . $docassemble_key);
40curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
41
42$result = curl_exec($ch);
43if (curl_errno($ch)) {
44 echo 'Error:' . curl_error($ch);
45}
46curl_close ($ch);
47
48$da_session = array();
49$da_session = json_decode($result, true);
50
51if (!(array_key_exists("secret", $da_session) & array_key_exists("session", $da_session))) {
52 die('Docassemble session creation failed. Could not get session id and secret from Docassemble API.');
53}
54
55$da_session_id = $da_session['session'];
56$da_secret = $da_session['secret'];
57
58// Set up another curl to attempt to assemble the document and retrieve the document id number.
59$pika_vars = array();
60// add more variables to pass here. Potentially could just pass the entire $case_row array
61$pika_vars['full_name'] = pl_grab_get('full_name');
62
63$postarray = [
64 'i' => $docassemble_interview,
65 'session' => $da_session_id,
66 'secret' => $da_secret,
67 'variables' => json_encode($pika_vars),
68];
69
70$ch_gen_doc = curl_init();
71
72curl_setopt($ch_gen_doc, CURLOPT_URL, $docassemble_url . '/api/session');
73curl_setopt($ch_gen_doc, CURLOPT_RETURNTRANSFER, 1);
74curl_setopt($ch_gen_doc, CURLOPT_POSTFIELDS, $postarray);
75
76
77$headers = array();
78array_push($headers, 'X-Api-Key: ' . $docassemble_key);
79//array_push($headers, 'Content-Type: application/json');
80curl_setopt($ch_gen_doc, CURLOPT_HTTPHEADER, $headers);
81
82$result_gen_doc = curl_exec($ch_gen_doc);
83if (curl_errno($ch_gen_doc)) {
84 echo 'Error:' . curl_error($ch_gen_doc);
85}
86curl_close ($ch_gen_doc);
87
88$file_info = array();
89$file_info = json_decode($result_gen_doc, true);
90
91if (!(isset($file_info["attachments"][0]["number"]["pdf"]) & isset($file_info["attachments"][0]["filename"]))) {
92 echo $result_gen_doc . PHP_EOL;
93 die('Could not generate file.');
94}
95
96$file_number = $file_info["attachments"][0]["number"]["pdf"];
97$file_name = $file_info["attachments"][0]["filename"];
98
99//this curl dumps back the interview variables to see if they are even passing in correctly. We only need this for debugging.
100/*
101$ch_vars = curl_init();
102curl_setopt($ch_vars, CURLOPT_URL, $docassemble_url . '/api/session?i=' . $docassemble_interview . '&session=' . $da_session_id . '&secret=' . $da_secret);
103curl_setopt($ch_vars, CURLOPT_RETURNTRANSFER, 1);
104
105curl_setopt($ch_vars, CURLOPT_HTTPHEADER, $headers);
106$result_vars = curl_exec($ch_vars);
107if (curl_errno($ch_vars)) {
108 echo 'Error:' . curl_error($ch_vars);
109}
110
111curl_close ($ch_vars);
112
113$interview_vars = array();
114$interview_vars = json_decode($result_vars, true);
115var_dump($result_vars);
116*/
117
118// the last curl will get the document
119$ch_get_doc = curl_init();
120curl_setopt($ch_get_doc, CURLOPT_URL, $docassemble_url . '/api/file/' . $file_number . '?i=' . $docassemble_interview . '&session=' . $da_session_id);
121curl_setopt($ch_get_doc, CURLOPT_RETURNTRANSFER, 1);
122
123curl_setopt($ch_get_doc, CURLOPT_HTTPHEADER, $headers);
124
125$result_get_doc = curl_exec($ch_get_doc);
126$curlcode = curl_getinfo($ch_get_doc, CURLINFO_HTTP_CODE);
127if (curl_errno($ch_get_doc)) {
128 echo 'Error:' . curl_error($ch_get_doc);
129}
130curl_close ($ch_get_doc);
131
132header('Content-Type: application/pdf');
133if (pl_grab_get('inline') == 'true') {
134 header('Content-Disposition: inline; filename="' . $file_name . '.pdf"');
135} else {
136 header('Content-Disposition: attachment; filename="' . $file_name . '.pdf"');
137}
138
139echo $result_get_doc;