-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathasynchronous_crcind.php
More file actions
161 lines (143 loc) · 5.82 KB
/
Copy pathasynchronous_crcind.php
File metadata and controls
161 lines (143 loc) · 5.82 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
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
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Meabed\ParallelSoap\ParallelSoapClient;
/**
* Live demo against the public crcind.com sample SOAP service:
* http://www.crcind.com/csp/samples/SOAP.Demo.cls?WSDL
*/
/** @var string $wsdl Public demo SOAP service used to exercise the client. */
$wsdl = "http://www.crcind.com/csp/samples/SOAP.Demo.cls?WSDL";
// parse response function
$parseResultFn = function ($method, $res) {
if (isset($res->{$method . 'Result'})) {
return $res->{$method . 'Result'};
}
return $res;
};
/** @var array $options , array of options for the soap client */
$options = [
'connection_timeout' => 40,
'trace' => true,
'exceptions' => true,
'soap_version' => SOAP_1_1,
'cache_wsdl' => WSDL_CACHE_BOTH,
'encoding' => 'UTF-8',
'resFn' => $parseResultFn,
];
/** @var ParallelSoapClient $client New Soap client instance */
$client = new ParallelSoapClient($wsdl, $options);
$client->setCurlOptions(
[CURLOPT_VERBOSE => false]
);
/**
* You can set debug mode to true to see curl verbose response if you run the script from command line
*
* @default false
*/
// $client->setDebug(false);
try {
$d = $client->LookupCity(['Mo', 'Meabed ']);
} catch (\Exception $e) {
// this is only available on trace=1 option in soapclient
$soapRequest = $client->__getLastRequest();
$exceptionMessage = $client->__getLastResponse();
}
/** Normal ONE SOAP CALL Using CURL same exact as soap synchronous api calls of any web service */
try {
$addInteger = $client->AddInteger(['Arg1' => 4, 'Arg2' => 3]);
print "AddInteger:: 4 + 3 = " . $addInteger . "\n";
} /** catch SoapFault exception if it happens */
catch (SoapFault $ex) {
print 'SoapFault: ' . $ex->faultcode . ' - ' . $ex->getMessage() . "\n";
} /** catch Exception if it happens */
catch (Exception $e) {
print 'Exception: ' . $e->getCode() . ' - ' . $e->getMessage() . "\n";
}
/**
* set SoapClient Mode to asynchronous mode.
* This will allow opening as many as connections to the host and perform all
* request at once so you don't need to wait for consecutive calls to performed after each other
*
* @default is false AND it get reset back to FALSE after each $client->run();
*
*/
$client->setMulti(true);
/** @var array $requestIds */
$requestIds = [];
/** in the next for loop i will make 5 soap request */
for ($i = 0; $i < 5; $i++) {
$requestIds[] = $client->LookupCity(['zip' => 90002 + $i * 100]);
// SoapFault: Client - looks like we got no XML document @todo add to test
// $requestIds[] = $client->LookupCity(['Mo', 'Meabed ' . $i]);
}
/** SoapCall without sessionId that return exception to test the exception handling */
$requestIds[] = $client->GetFullName('wrongParam', 'Dummy');
/**
* This call will throw SoapFault and it will return
* string as ".. Function ("getUnkownMethod") is not a valid method for this service...."
* So it will be easy to debug it.
*
* @note The call will not be executed when $client->run()
*/
$requestIds[] = $client->UnkownMethod(['wrongParam', 'Dummy']);
/**
* This call will throw SoapFault and it will return
* string as ".. Function ("getAnotherUnkownMethod") is not a valid method for this service...."
* So it will be easy to debug it.
*
* @note The call will not be executed when $client->run()
*/
$requestIds[] = $client->AnotherUnkownMethod(['dummy' => 'test']);
/**
* This call is valid method but it has wrong parameters so it will return normal request id but in the execution
* it will return result instance of SoapFault contains the exception
* So you can handle it
*/
$requestIds[] = $client->QueryByName(['name' => 'Allen']);
/**
* Adding another 5 SoapCalls to test different method call
* in the next for loop i will make 5 soap request
*/
for ($i = 0; $i < 5; $i++) {
/** @var $params , method parameters will be used in the test */
$requestIds[] = $client->FindPerson(['name' => 'Name ' . $i]);
}
/** You can see the request ids in the variable that will be executed with $client->run() method */
print_r($requestIds);
/**
* You can execute certain requests if you pass array of requestIds to $client->run() method as in the example in the
* comment
* $client->run(array(0,2,3,6,7)); , This will execute this requests only from the 10 requests we did before
*/
/** @var $responses array that hold the response array as array( requestId => responseObject ); */
$responses = $client->run();
/** Loop through the responses and get the results */
foreach ($responses as $id => $response) {
/**
* Handle exception when you using multi request is different than normal requests
* The Client in asynchronous mode already handle the exception and assign the exception object to the result in-case exception occurred
* So to handle the exception we don't use try{}catch(){} here, but we use instanceof to handle the exceptions as the example below
*/
if ($response instanceof SoapFault) {
/** handle the exception here */
print 'SoapFault: ' . $response->faultcode . ' - ' . $response->getMessage() . "\n";
} else {
/** SoapResponse is Okay */
/**
* I have made the SoapServer always return the Response in class attribute public $Return
* Usually the soap server has pattern to return response for all method calls
*
* @example
* if i call method getUser the return object will be $response->getUserResponse
* getName => $response->getNameResponse
* logout => $response->logoutResponse
*
* @Important please check ParallelSoapClient NOTES in @line 153 and @line 295 For auto implementation of the soap response pattern
*
*/
if (!is_string($response)) {
$response = json_encode($response, JSON_UNESCAPED_SLASHES);
}
print 'Response is : ' . $response . "\n";
}
}