Implementing simple Web Services with PHP SOAP extensions

Source: Internet
Author: User
Tags soap wsdl

Original: Implementing Simple Web Services with PHP SOAP extensions

What can webservices do?

WebServices can convert an application to a network application.

By using WebServices, your application can publish information to the world or provide a feature.

Well, about webservices online material many, not too much introduction, directly into the subject.

PHP has two extension class libraries can be implemented WebServices, one is Nusoap, one is the official PHP SOAP extension, in the use of roughly the same, take the official own SOAP extension.

The Soapclient,soapserver,soapfault three classes are used primarily in soap authoring webservices.

SoapClient: The class that the user accesses, that is, the client, using the WebServices class

SoapServer: Provide webservices class, service side

SoapFault: Exception Handling Class

As an example, write a super-simple webservices to feel it, direct code to speak

1, Myself.class.php--Business logic class, function realization class

1 <?PHP2     //Business logic Class 3 class     Myself{4 public         function info () {5             return "Sina Weibo: Balla_ Rabbit, ask attention ~"; 6         }7     }8?>

It's easy to return a string without unnecessary explanation.

2, soapserver.php--Service End class, provide service

1 <?php 2     //server 3     require_once (' Myself.class.php '); 4     $parameter =array (5         ' uri ' = '/ localhost/', 6         ' location ' = ' http://localhost/soap/soapServer.php ' 7         ); 8     $soapServer =new SoapServer (null, $parameter); 9     $soapServer->setclass (' myself '), ten     $soapServer->handle ();?>

The SoapServer has two modes of operation:

The above example is the NON-WSDL mode, when instantiating the SoapServer class, a parameter is to put the WSDL file, in non-wsdl mode, can be empty, the configuration parameters in the form of an array in the second parameter.

If you are using WSDL mode, you can let the server read the configuration parameters directly with the WSDL file, and you can omit the second array parameter.

There are many configuration parameters, the above for a simple example only listed 2, specifically, we can check online

Uri--namespace

Location--Service address

1. wsdl mode in 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. non-wsdl mode in non-wsdl mode, use parameters to pass the information to be used to manage the behavior of the service.

Among the many methods of the SoapServer class, there are three methods that are more important. They are Soapserver::setclass (), Soapserver::addfunction (), Soapserver::handle ().

It is particularly important to note that no parameters can be output after the handle method, or an error occurs.

3, soapclient.php--client class, using the service

1 <?php 2     //client 3     $parameter =array (4         ' uri ' = ' http://localhost/', 5         ' location ' =/ localhost/soap/soapserver.php ' 6         ); 7     try{8         $soapClient =new soapclient (null, $parameter), 9         echo $soapClient->info (),     }catch ( Exception $e) {         echo $e->getmessage ()     }14?>

The SoapClient class can act as a client for a given webservices.
It has two modes of operation: (similar to the two models of SoapServer)
1. WSDL mode

2. NON-WSDL mode

These are simple webservices that are implemented with PHP SOAP extensions, and we visit soapclient.php to try

Easy to fix ~

Report:

Now the return value of most webservices interfaces is JSON or XML, and we can define our own assignments internally.

Common functions file_get_contents, Json_encode, Json_decode, etc.

Here's a note on JSON operations: http://www.cnblogs.com/lichenwei/p/3888586.html

Implementing simple Web Services with PHP SOAP extensions

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.