Simple Web Services using php soap Extension

Source: Internet
Author: User

Simple Web Services using php soap Extension
What can WebServices do? WebServices can convert applications into network applications. By using WebServices, your applications can publish information to the world or provide a function. Well, there are a lot of online information about WebServices, so I just want to introduce it more and go directly to the topic. PHP has two extension libraries to implement WebServices. One is NuSoap and the other is the Soap extension officially provided by php, which is roughly the same in usage. Let's talk about the official Soap extension. The SoapClient, SoapServer, and SoapFault classes are mainly used for compiling WebServices in Soap. SoapClient: the class accessed by the user, that is, the client. Use the WebServices class SoapServer: Provides the WebServices class, and the server SoapFault: exception handling class as an example. Write a super simple WebServices, direct code talk 1. Myself. class. php-business logic class, function implementation class copy code 1 <? Php2 // business logic class 3 class Myself {4 public function info () {5 return "Sina Weibo: Balla _ rabbit, please pay attention to it ~ "; 6} 7} 8?> Copying the code is simple and requires no extra explanation. A string is returned. 2. soapServer. php-server class, providing service copy code 1 <? Php 2 // server 3 require_once ('myself. class. php '); 4 $ parameter = array (5 'url' => 'HTTP: // localhost/', 6 'location' => 'HTTP: // localhost/soap/soapServer. php '7); 8 $ soapServer = new SoapServer (null, $ parameter); 9 $ soapServer-> setClass ('myself '); 10 $ soapServer-> handle (); 11?> The replication code SoapServer has two operation modes: the preceding example shows the non-WSDL mode. when instantiating the SoapServer class, a parameter is placed in the WSDL file. In the non-WSDL mode, it can be blank. The configuration parameters are written in the form of an array in the second parameter. If the WSDL mode is used, you can directly use the WSDL file to let the server read the configuration parameters. In this case, the second array parameter can be omitted. There are many configuration parameters. For the simple example above, only two parameters are listed. You can check uri -- namespace location -- service address on the Internet. 1. The WSDL mode is in the WSDL mode, the constructor can use the WSDL file name as a parameter and extract the information used by the Service from the WSDL. 2. In non-WSDL mode, parameters are used to pass the information to be used to manage Service behaviors. Among the many methods of the SoapServer class, three methods are important. They are SoapServer: setClass (), SoapServer: addFunction (), SoapServer: handle (). Note that no parameters can be output after the handle method. Otherwise, an error occurs. 3. soapClient. php-client class, use the service to copy code 1 <? Php 2 // client 3 $ parameter = array (4 'url' => 'HTTP: // localhost/', 5 'location' => 'HTTP: // localhost/soap/soapServer. php '6); 7 try {8 $ soapClient = new SoapClient (null, $ parameter); 9 echo $ soapClient-> info (); 10 11} catch (Exception $ e) {12 echo $ e-> getMessage (); 13} 14 15?> The copy code SoapClient class can be used as a client for a given WebServices.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.