-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathquickstart.php
More file actions
71 lines (59 loc) · 2.4 KB
/
Copy pathquickstart.php
File metadata and controls
71 lines (59 loc) · 2.4 KB
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
<?php
/**
* Quick-start template for meabed/php-parallel-soap.
*
* Point $wsdl at your own SOAP service and run:
*
* php example/quickstart.php
*
* For a fully runnable, offline example see the hermetic test suite in tests/Hermetic,
* which boots a local SOAP server (HTTP and TLS) and calls it through this client.
*/
require_once __DIR__ . '/../vendor/autoload.php';
use Meabed\ParallelSoap\ParallelSoapClient;
/** @var string $wsdl Replace with the WSDL of the service you want to call. */
$wsdl = 'https://example.com/service?wsdl';
$options = [
'trace' => true,
'exceptions' => true,
'soap_version' => SOAP_1_1,
'cache_wsdl' => WSDL_CACHE_BOTH,
'encoding' => 'UTF-8',
// Optional: unwrap the "<MethodResult>" envelope into a scalar/object value.
'resFn' => static fn ($method, $res) => $res->{$method . 'Result'} ?? $res,
];
$client = new ParallelSoapClient($wsdl, $options);
/* -------------------------------------------------------------------------
* 1) Synchronous call — behaves exactly like the native SoapClient.
* ---------------------------------------------------------------------- */
$client->setMulti(false);
try {
$result = $client->SomeMethod(['arg1' => 1, 'arg2' => 2]);
echo 'Sync result: ' . print_r($result, true) . "\n";
} catch (SoapFault $ex) {
echo 'SoapFault: ' . $ex->faultcode . ' - ' . $ex->getMessage() . "\n";
}
/* -------------------------------------------------------------------------
* 2) Parallel calls — queue many requests, then run() them concurrently.
* In parallel mode each call returns a request id instead of a result,
* and run() returns an array keyed by those ids.
* ---------------------------------------------------------------------- */
$client->setMulti(true);
$client->setCurlOptions([
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
]);
$requestIds = [];
for ($i = 0; $i < 5; $i++) {
$requestIds[] = $client->SomeMethod(['arg1' => $i, 'arg2' => $i + 1]);
}
// Fire all queued requests at once. The client resets to single mode afterwards.
$responses = $client->run();
foreach ($responses as $id => $response) {
if ($response instanceof SoapFault) {
// In parallel mode faults are returned, not thrown.
echo "Error {$id}: {$response->getMessage()}\n";
continue;
}
echo "OK {$id}: " . (is_string($response) ? $response : json_encode($response)) . "\n";
}