PHP Write WebService service side

Source: Internet
Author: User
Tags soap php write sprintf

1) WebServiceTechnical Introduction

WebService is a remote invocation technique across programming languages and cross-operating system platforms. Only through the Web Service, the client and server are free to communicate with HTTP . Regardless of the platform of two programs and become the language is what.

XML,SOAP , and WSDL are the three major technologies of the Web Service Platform:

WebService uses HTTP Protocol data transfer. encapsulates the data in XML format , which describes which method of invoking a remote service object, what is passed, and what the return result of the service object is.

XML is a WebService the format in which the data is represented in the platform. In addition to being easy to establish and easy to analyze,XML is basically a bit of a platform-independent, vendor-independent.

Soap,WebServicethroughHTTPwhen the protocol sends the request and receives the result. Both the requested content and the resulting content are sentXMLformat, and adds a number of specificHTTPThe message header. To explainHTTPthe content format of the message. These specificHTTPmessage headers andXMLThe content format isSOAPagreement. SOAPprovides a standardRPCmethod to invoke theWeb Service.

SOAP Protocol = HTTP Protocol + XML data format.

Wsdl ( web service  Description language ) is based on xml web service and its functions, parameters, and return values, which are webservice client and the server side can all understand the standard format.

Because it is based on xml wsdl timely machine readable. People can read it again. wsdl file saved in web server, via a url address to be able to interview it. The client wants to call a webservice Service, be aware of the service's wsdl

WebService There are two ways that a service provider can expose its WSDL file Address: 1 , note to UUID server in order to be looked up by others; 2 , which is directly told to the client caller.

2) ApacheRelated Configuration

In order to be able to implement Web Service. httpd must support soap extensions, where yum installs httpd and Soap extension as an example to illustrate:

1> installation httpd

Yum Install httpd

2> installing php

Yum Install PHP

3> configuration httpd support php parsing

Add for example the following in /etc/httpd/conf/httpd.conf :

AddType application/x-httpd-php. php

AddType Application/x-httpd-php-source. Phps

4> Another start httpd

Service httpd Restart

5> Check if the soap extension is installed

/usr/bin/php-m | grep soap//! Assuming that there is already an installed Soap extension, assuming that it does not exist, run the next

6> Installing the soap extension

Yum Install Php-soap//! Check again after the installation is complete. Restart httpd

7> Verifying that the soap extension is installed correctly

It can be done through php-m | grep soap , or through phpinfo ();

3) PHPImplementWebServiceService Side

1> Definition Service class--service.php

The server is implementing the interface you want to provide externally, and if we need to provide a function externally, it receives a string. Then return "Hello" + input string This small function, the service class is very easy, such as the following see:

<?

PHP class Service {public Function SayHello ($content) { return ' Hello '. $content;} }? >

2> generating wsdl file--create_wsdl.php

In order to generate the WSDL file, we are able to use the getwsdl in the SoapDiscovery.class.php provided online Provides the method to implement. The SoapDiscovery.class.php code can be downloaded online, in order to generate files, we need to change getwsdl Part of the code in the method (plus the code that writes the file), as seen in the following:

return sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ' </ Definitions> ');//! gaze out the return and add code such as the following. The purpose is to call getwsdl to generate a. wsdl file $fso = fopen ($this->class_name. "). WSDL "," w "); Fwrite ($fso, sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL , ' </definitions> ');                        Fclose ($FSO);

Now that we can define our own create_wsdl.php , we just need to call this method. For example, the following:

<?php include ("service.php"); Include ("SoapDiscovery.class.php");//! The first parameter is a class name. The generated WSDL file is named after it. The second parameter is the name of the service, which can be casually written $disco = new Soapdiscovery (' service ', ' soap '); $disco->getwsdl ();

>

3> Change Service side--service.php

Change the service.php. Add some code, such as the following:

<?php   class Service   {public Function SayHello ($content) {      return ' Hello '. $content;}   }     $service = new SoapServer (' http://192.168.17.129/soap/Service.wsdl ', Array (' soap_version ' = soap_1_2));   $service->setclass ("service"); //! All methods of service class   $service->handle ();//! processing Request?>

4> Change service.wsdl file

5> Client Test--client.php

Client.php is used to test whether the service side of the Web Server We just wrote is available, such as the following code:

<?

php $soap = new SoapClient (' http://192.168.17.129/soap/Service.php?

WSDL '); echo $soap->sayhello ("houqd2012");? >

The output results are as follows:

Hello houqd2012

PHP Write WebService service side

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.