Original: SOAP Application in PHP
SOAP: Simple Object Access Protocol
(Soap:simple Object Access Protocol)
Simple Object Access Protocol (SOAP) is a lightweight, simple, XML-based protocol that is designed to exchange structured and solidified information on the WEB. SOAP can be used in conjunction with many existing Internet protocols and formats, including Hypertext Transfer Protocol (HTTP), Simple Mail Transfer Protocol (SMTP), Multipurpose Internet Mail Expansion Protocol (MIME). It also supports a large number of applications, from the messaging system to Remote procedure calls (RPCs).
Here is a simple example of how soap is used in PHP.
1. Use PHP to provide SOAP interface to other people
To use PHP to provide a SOAP interface, the only thing we need to do is declare an interface using the SoapServer class.
Here is the sample code:
PHP File: soapinterfaceprovider.php
<?php/** * * */function Getsoapdata () {$data = ' <?xml version= ' 1.0 ' encoding= ' UTF8 ' to be provided to the user? >& Lt;data>hello world<data> '; return $data; }//The first parameter represents the WSDL, the second argument URI parameter represents the namespace $soap = new SoapServer (null, array (' uri ' = ' http://www.qqstore.net/soap/')); Declares an interface $soap, AddFunction ("Getsoapdata") for user invocation; $soap-handle ();? >
2. Call the SOAP interface already provided in PHP
To test the interface provided in step 1, simply execute the following test code. ( just execute the following code without having to start or execute it beforehand .)
soapinterfaceprovider.php file , PHP will automatically execute the soapinterfaceprovider.php file when the following test code is called. So
The location parameter here is very important, can not be wrong, must be actually place soapinterfaceprovider.php position)
PHP File: soapclient.php
<?php/* creates a SOAP client, the first parameter represents the WSDL, and the location in the second parameter represents the position of the PHP file that defines the SoapServer, and the URI parameter represents the discovery in the namespace test. The URI here is not a problem even if it is not the same as defined in soapinterfaceprovide.php, it may be related to the only SOAP interface I have. */$client = new SoapClient (null, Array (' location ' = ' http://localhost/webcenter/soap/SoapInterfaceProvider.php ', ' uri ' = ' http://www.qqstore.net/soap/');//Call Getsoapdata interface $data = $client getsoapdata (); echo "Get to Data:". $data;? >
Enter the following connection in the browser to test: http://localhost/soap/SoapClient.php (adjust according to the actual location)
Execution Result:
get to Data: <?xml version= "1.0" encoding= "Utf-8"? ><data>hello world<data>
3. Code optimization
There is only one method in the file soapinterfaceprovider.php of the SOAP interface provided above, and if there are many ways to provide it, it is a bit of a hassle to call the addfunction method every time. And it's not appropriate to write all the actual methods in the soapinterfaceprovider.php file.
Here we can put all the actual methods that we want to invoke into a single PHP class, and declare all the interfaces once using the SetClass method.
Here is the sample code:
PHP File: SoapMethod.class.php
<?phpclass Soapmethod {/** * interface to be provided to the user * */function Getsoapdata () {$data = ' <? XML version= "1.0" encoding= "Utf-8"? ><data>hello world<data> '; return $data; /** * interface to be supplied to the user 1 * */function GetSoapData1 () {$data 1 = ' <?xml version= "1.0" encoding= "u Tf-8 "><data>hello world<data>"; return $data 1; }}?>
PHP File: soapinterfaceprovider.php
<?php//Introduce the Class require_once './function.class.php ' that contains the method that was actually called;//The first parameter represents the WSDL, the second argument URI parameter represents the namespace $soap = new SoapServer (NULL, array (' uri ' = ' http://www.qqstore.net/soap/'));//Declares an interface $soap-SetClass ("Soapmethod") for user invocation; $soap-handle ();? >
When I was testing, I didn't use the SetClass method at first, but I used the AddFunction method, and the method I actually called was put into the Soapmethod class, and
The SoapMethod.class.php code is the same as above. Results when you call the AddFunction method, the following error occurs:
Fatal error: uncaught soapfault exception: [HTTP] not Found in C:/appserv/www/webcenter/soap/soapclient.php:4 Stack Trace: #0 [internal function]: soapclient->__dorequest (' <?xml version= ' ... ', ' Http://localhos ... ', ' http:/ /www.qqst ', 1, 0) #1 [internal function]: Soapclient->__call (' Getsoapdata ', Array) #2 c:/appserv/www/webcenter/ Soap/soapclient.php (4): Soapclient->getsoapdata () #3 {main} thrown in c:/appserv/www/webcenter/soap/ soapclient.php on line 4
After discovery is used in the method definition in the class, the soapinterfaceprovider.php file cannot be found. Remove the class definition from the SoapMethod.class.php, leaving only the fixed
The method of righteousness is possible.
The above error will also occur if there is a problem with the location parameter passed in when creating the soapclient.
SOAP Application in PHP