This article offers a second approach to using LiveDocx in PHP without the Zend Framework. This one does not require the PHP 5 SOAP extension, but does require the SOAP library NuSOAP.

The majority of the code on this page was contributed by Mats Andersson. He took the original code and ported it to work with NuSOAP. A big thank you goes out for his work!

Download and install NuSOAP

Visit the NuSOAP homepage and download the current version of NuSOAP. For the purpose of this article, we are using v0.7.3. Your mileage may vary for other versions. Then, set the include path (see line, containing set_include_path()) to the path in which NuSOAP is installed.

Download templates

Once you have the libraries in place, download the following template files. They are used by the sample applications:

Copy them into the same directory as the executable file containing the PHP code.

Set your username and password

Specify the constants USERNAME and PASSWORD, which you received after signing up for an account.

Code discussion

At first glance, the code looks more complicated than in the original article.

The way in which complex data types (associative arrays) are handled by NuSOAP and the backend service is somewhat tricky. In particular, take a look at the source code to the following functions:

1
2
function nuSoap_assocArrayToArrayOfArrayOfString ($assoc)
function nuSoap_multiAssocArrayToArrayOfArrayOfString ($multi)

Moreover, the way in which parameters are passed to SOAP methods with NuSOAP is even more nested than using the native PHP 5 SOAP client.

Consider (NuSOAP):

1
2
3
4
5
6
$result = $nuSoap->call('GetAllBitmaps',
    array(
        'zoomFactor' => 100,
        'format'     => 'png'
    )
);

vs (PHP 5 SOAP client):

1
2
3
4
5
6
$result = $soap->GetAllBitmaps(
    array(
        'zoomFactor' => 100,
        'format'     => 'png'
    )
);

However, with the examples in this article, it should be relatively easy to expand the code and integrate LiveDocx into your own application on platforms, which do not have the PHP 5 SOAP extension installed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/php
 
// Turn up error reporting
error_reporting (E_ALL ^ E_DEPRECATED);
 
// Turn off WSDL caching
ini_set ('soap.wsdl_cache_enabled', 0);
 
// Define credentials for LD
define ('USERNAME', 'yourUsername');
define ('PASSWORD', 'yourPassword');
 
// SOAP WSDL endpoint
define ('ENDPOINT', 'https://api.livedocx.com/1.2/mailmerge.asmx?WSDL');
 
// Define timezone
date_default_timezone_set('Europe/Berlin');
 
// Set include path
set_include_path (dirname(__FILE__) . '/nusoap-0.7.3/lib');
 
// Include NuSOAP libary
require_once 'nusoap.php';
 
//-----------------------------------------------------------------------------
 
//
// SAMPLE #1 - License Agreement
//
 
print('Starting sample #1 (license-agreement)...');
 
// Instantiate SOAP object and log into LiveDocx
 
$nuSoap = new nusoap_client(ENDPOINT, true);
 
// Set charset encoding for outgoing messages
 
$nuSoap->soap_defencoding = 'UTF-8';
 
// Log into SOAP service
 
$nuSoap->call('LogIn',
    array(
        'username' => USERNAME,
        'password' => PASSWORD
    )
);
 
// Upload template
 
$data = file_get_contents('./license-agreement-template.docx');
 
$nuSoap->call('SetLocalTemplate',
    array(
        'template' => base64_encode($data),
        'format'   => 'docx'
    )
);
 
// Assign data to template
 
$fieldValues = array (
    'software' => 'Magic Graphical Compression Suite v2.5',
    'licensee' => 'Henry Döner-Meyer',
    'company'  => 'Megasoft Co-Operation',
    'date'     => date('F d, Y'),
    'time'     => date('H:i:s'),
    'city'     => 'Berlin',
    'country'  => 'Germany'
);
 
$result = $nuSoap->call('SetFieldValues',
    array(
        'fieldValues' => nuSoap_assocArrayToArrayOfArrayOfString($fieldValues)
    )
);
 
// Build the document
 
$nuSoap->call('CreateDocument');
 
// Get document as PDF
 
$result = $nuSoap->call('RetrieveDocument',
    array(
        'format' => 'pdf'
    )
);
 
$data = $result['RetrieveDocumentResult'];
 
file_put_contents('./license-agreement-document.pdf', base64_decode($data));
 
// Get document as bitmaps (one per page)
 
$result = $nuSoap->call('GetAllBitmaps',
    array(
        'zoomFactor' => 100,
        'format'     => 'png'
    )
);
 
$data = array();
 
if (isset($result['GetAllBitmapsResult']['string'])) {
    $pageCounter = 1;
    if (is_array($result['GetAllBitmapsResult']['string'])) {
        foreach ($result['GetAllBitmapsResult']['string'] as $string) {
            $data[$pageCounter] = base64_decode($string);
            $pageCounter++;
        }
    } else {
       $data[$pageCounter] = base64_decode($result['GetAllBitmapsResult']['string']);
    }
}
 
foreach ($data as $pageCounter => $pageData) {
    $pageFilename = sprintf('./license-agreement-document-page-%s.png', $pageCounter);
    file_put_contents($pageFilename, $pageData);
}
 
// Get document as Windows metafiles (one per page)
 
$result = $nuSoap->call('GetAllMetafiles',
    array(
        'zoomFactor' => 100,
        'format'     => 'png'
    )
);
 
$data = array();
 
if (isset($result['GetAllMetafilesResult']['string'])) {
    $pageCounter = 1;
    if (is_array($result['GetAllMetafilesResult']['string'])) {
        foreach ($result['GetAllMetafilesResult']['string'] as $string) {
            $data[$pageCounter] = base64_decode($string);
            $pageCounter++;
        }
    } else {
       $data[$pageCounter] = base64_decode($result['GetAllMetafilesResult']['string']);
    }
}
 
foreach ($data as $pageCounter => $pageData) {
    $pageFilename = sprintf('./license-agreement-document-page-%s.wmf', $pageCounter);
    file_put_contents($pageFilename, $pageData);
}
 
// Log out (closes connection to backend server)
 
$nuSoap->call('LogOut');
 
unset($nuSoap);
 
print('DONE.' . PHP_EOL);
 
// -----------------------------------------------------------------------------
 
//
// SAMPLE #2 - Telephone Bill
//
 
print('Starting sample #2 (telephone-bill)...');
 
// Instantiate nuSOAP Client
 
$nuSoap = new nusoap_client(ENDPOINT, true);
 
// Set charset encoding for outgoing messages
 
$nuSoap->soap_defencoding = 'UTF-8';
 
// Log into SOAP service
 
$nuSoap->call('LogIn',
    array(
        'username' => USERNAME,
        'password' => PASSWORD
    )
);
 
// Upload template
 
$data = file_get_contents('./telephone-bill-template.doc');
 
// Assign field values data to template
 
$nuSoap->call('SetLocalTemplate',
    array(
        'template' => base64_encode($data),
        'format'   => 'doc'
    )
);
 
$fieldValues = array (
    'customer_number' => sprintf("#%'10s",  rand(0,1000000000)),
    'invoice_number'  => sprintf("#%'10s",  rand(0,1000000000)),
    'account_number'  => sprintf("#%'10s",  rand(0,1000000000)),
    'phone'           => '+49 421 335 9000',
    'date'            => date('F d, Y'),
    'name'            => 'James Dööner',
    'service_phone'   => '+49 421 335 910',
    'service_fax'     => '+49 421 335 9180',
    'month'           => date('F Y'),
    'monthly_fee'     =>  '€ 15.00',
    'total_net'       => '€ 100.00',
    'tax'             =>    '19%',
    'tax_value'       =>  '€ 15.00',
    'total'           => '€ 130.00'
);
 
$result = $nuSoap->call('SetFieldValues',
    array(
        'fieldValues' => nuSoap_assocArrayToArrayOfArrayOfString($fieldValues)
    )
);
 
$blockFieldValues = array (
    array ('connection_number' => '+49 421 335 912', 'connection_duration' => '00:00:07', 'fee' => '€ 0.03'),
    array ('connection_number' => '+49 421 335 913', 'connection_duration' => '00:00:07', 'fee' => '€ 0.03'),
    array ('connection_number' => '+49 421 335 914', 'connection_duration' => '00:00:07', 'fee' => '€ 0.03'),
    array ('connection_number' => '+49 421 335 916', 'connection_duration' => '00:00:07', 'fee' => '€ 0.03')
);
 
$nuSoap->call('SetBlockFieldValues',
    array (
        'blockName'        => 'connection',
        'blockFieldValues' => nuSoap_multiAssocArrayToArrayOfArrayOfString($blockFieldValues)
    )
);
 
// Build the document
 
$nuSoap->call('CreateDocument');
 
// Get document as PDF
 
$result = $nuSoap->call('RetrieveDocument',
    array(
        'format' => 'pdf'
    )
);
 
$data = $result['RetrieveDocumentResult'];
 
file_put_contents('./telephone-bill-document.pdf', base64_decode($data));
 
// Log out (closes connection to backend server)
 
$nuSoap->call('LogOut');
 
unset($nuSoap);
 
print('DONE.' . PHP_EOL);
 
//-----------------------------------------------------------------------------
 
/**
 * Convert a PHP assoc array to a SOAP array of array of string, suitable for NuSOAP
 *
 * @param array $assoc
 * @return array
 */
function nuSoap_assocArrayToArrayOfArrayOfString ($assoc)
{
    $arrayKeys   = array_keys($assoc);
    $arrayValues = array_values($assoc);
 
    $result = array('ArrayOfString' => array(
                  array('string' => $arrayKeys) ,
                  array('string' => $arrayValues)) );
 
    return $result;
}
 
/**
 * Convert a PHP multi-depth assoc array to a SOAP array of array of array of string, suitable for NuSOAP
 *
 * @param array $multi
 * @return array
 */
function nuSoap_multiAssocArrayToArrayOfArrayOfString ($multi)
{
    $arrayKeys = array_keys($multi[0]);
 
    $arrayValues = array();
    foreach ($multi as $v) {
        $arrayValues[] = array_values($v);
    }
 
    $_arrayKeys = array();
    $_arrayKeys[0] = $arrayKeys;
 
    $result = array('ArrayOfString' => array(array('string' => $arrayKeys)));
 
    foreach ($arrayValues as $a) {
        $result['ArrayOfString'][] = array('string' => $a);
    }
 
    return $result;
}

API documentation

You can read full API documentation here and the current WSDL here.

Need help?

Please post all requests for support into the phpLiveDocx support forum.

原文取自: http://www.phplivedocx.org/articles/using-livedocx-with-nusoap/ 


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Frank 的頭像
    Frank

    經驗交流分享與備忘

    Frank 發表在 痞客邦 留言(0) 人氣()