1 | <?php |
---|
2 | /** |
---|
3 | * This file contains an example SOAP client that calls methods on the example |
---|
4 | * SOAP server in this same directory. |
---|
5 | * |
---|
6 | * PHP versions 4 and 5 |
---|
7 | * |
---|
8 | * LICENSE: This source file is subject to version 2.02 of the PHP license, |
---|
9 | * that is bundled with this package in the file LICENSE, and is available at |
---|
10 | * through the world-wide-web at http://www.php.net/license/2_02.txt. If you |
---|
11 | * did not receive a copy of the PHP license and are unable to obtain it |
---|
12 | * through the world-wide-web, please send a note to license@php.net so we can |
---|
13 | * mail you a copy immediately. |
---|
14 | * |
---|
15 | * @category Web Services |
---|
16 | * @package SOAP |
---|
17 | * @author Shane Caraveo <Shane@Caraveo.com> Port to PEAR and more |
---|
18 | * @author Jan Schneider <jan@horde.org> Maintenance |
---|
19 | * @copyright 2003-2007 The PHP Group |
---|
20 | * @license http://www.php.net/license/2_02.txt PHP License 2.02 |
---|
21 | * @link http://pear.php.net/package/SOAP |
---|
22 | */ |
---|
23 | |
---|
24 | /** SOAP_Client */ |
---|
25 | require 'SOAP/Client.php'; |
---|
26 | |
---|
27 | /* This client runs against the example server in SOAP/example/server.php. It |
---|
28 | * does not use WSDL to run these requests, but that can be changed easily by |
---|
29 | * simply adding '?wsdl' to the end of the url. */ |
---|
30 | $soapclient = new SOAP_Client('http://localhost/SOAP/example/server.php'); |
---|
31 | |
---|
32 | /* Set a few options. */ |
---|
33 | $options = array(); |
---|
34 | |
---|
35 | /* This namespace is the same as declared in server.php. */ |
---|
36 | $options['namespace'] = 'urn:SOAP_Example_Server'; |
---|
37 | |
---|
38 | /* Trace the communication for debugging purposes, so we can later inspect the |
---|
39 | * data with getWire(). */ |
---|
40 | $options['trace'] = true; |
---|
41 | |
---|
42 | /* Uncomment the following lines if you want to use Basic HTTP |
---|
43 | * Authentication. */ |
---|
44 | // $options['user'] = 'username'; |
---|
45 | // $options['pass'] = 'password'; |
---|
46 | |
---|
47 | header('Content-Type: text/plain'); |
---|
48 | |
---|
49 | /* Calling echoStringSimple. */ |
---|
50 | $ret = $soapclient->call('echoStringSimple', |
---|
51 | array('inputStringSimple' => 'this is a test string'), |
---|
52 | $options); |
---|
53 | // echo $soapclient->getWire(); |
---|
54 | print_r($ret); |
---|
55 | echo "\n"; |
---|
56 | |
---|
57 | /* Calling echoString. */ |
---|
58 | $ret = $soapclient->call('echoString', |
---|
59 | array('inputString' => 'this is a test string'), |
---|
60 | $options); |
---|
61 | // echo $soapclient->getWire(); |
---|
62 | print_r($ret); |
---|
63 | echo "\n"; |
---|
64 | |
---|
65 | /* Calling divide with valid parameters. */ |
---|
66 | $ret = $soapclient->call('divide', |
---|
67 | array('dividend' => 22, 'divisor' => 7), |
---|
68 | $options); |
---|
69 | // echo $soapclient->getWire(); |
---|
70 | if (PEAR::isError($ret)) { |
---|
71 | echo 'Error: ' . $ret->getMessage(); |
---|
72 | } else { |
---|
73 | echo 'Quotient is ' . $ret; |
---|
74 | } |
---|
75 | echo "\n"; |
---|
76 | |
---|
77 | /* Calling divide with invalid parameters. */ |
---|
78 | $ret = $soapclient->call('divide', |
---|
79 | array('dividend' => 22, 'divisor' => 0), |
---|
80 | $options); |
---|
81 | // echo $soapclient->getWire(); |
---|
82 | if (PEAR::isError($ret)) { |
---|
83 | echo 'Error: ' . $ret->getMessage(); |
---|
84 | } else { |
---|
85 | echo 'Quotient is ' . $ret; |
---|
86 | } |
---|
87 | echo "\n"; |
---|
88 | |
---|
89 | /* The SOAPStruct class is defined in example_types.php. */ |
---|
90 | require_once 'example_types.php'; |
---|
91 | $struct = new SOAPStruct('test string', 123, 123.123); |
---|
92 | |
---|
93 | /* Send an object, get an object back. Tell the client to translate to classes |
---|
94 | * we provide if possible. */ |
---|
95 | $soapclient->_auto_translation = true; |
---|
96 | |
---|
97 | /* You can explicitly set the translation for a specific |
---|
98 | * class. auto_translation works for all cases, but opens ANY class in the |
---|
99 | * script to be used as a data type, and may not be desireable. Both can be |
---|
100 | * used on client or server. */ |
---|
101 | $soapclient->setTypeTranslation('{http://soapinterop.org/xsd}SOAPStruct', |
---|
102 | 'SOAPStruct'); |
---|
103 | |
---|
104 | /* Calling echoStruct. */ |
---|
105 | $ret = $soapclient->call('echoStruct', |
---|
106 | array('inputStruct' => $struct->__to_soap()), |
---|
107 | $options); |
---|
108 | // echo $soapclient->getWire(); |
---|
109 | print_r($ret); |
---|
110 | |
---|
111 | /* Calling echoStructAsSimpleTypes. |
---|
112 | * PHP doesn't support multiple return values in function calls, so we must do |
---|
113 | * a little work to make it happen here, for example returning an array |
---|
114 | * instead. This requires knowledge on the developers part to figure out how |
---|
115 | * they want to deal with it. */ |
---|
116 | $ret = $soapclient->call('echoStructAsSimpleTypes', |
---|
117 | array('inputStruct' => $struct->__to_soap()), |
---|
118 | $options); |
---|
119 | // echo $soapclient->getWire(); |
---|
120 | if (PEAR::isError($ret)) { |
---|
121 | echo 'Error: ' . $ret->getMessage(); |
---|
122 | } else { |
---|
123 | list($string, $int, $float) = array_values($ret); |
---|
124 | echo "varString: $string\nvarInt: $int\nvarFloat: $float"; |
---|
125 | } |
---|
126 | echo "\n"; |
---|
127 | |
---|
128 | /* Calling echoMimeAttachment. |
---|
129 | * We want to use MIME encoding here, the default is to use DIME encoding. */ |
---|
130 | $options['attachments'] = 'Mime'; |
---|
131 | $attachment = new SOAP_Attachment('attachment', 'text/plain', null, |
---|
132 | 'This is a MIME attachment'); |
---|
133 | $ret = $soapclient->call('echoMimeAttachment', |
---|
134 | array($attachment), |
---|
135 | $options); |
---|
136 | // echo $soapclient->getWire(); |
---|
137 | print_r($ret); |
---|