PHP: webservice server, webservice Server

Source: Internet
Author: User

PHP: webservice server, webservice Server
1) Introduction to WebService technology

WebService is a remote call technology that supports cross-programming languages and cross-operating system platforms. Only through Web Service can the client and server communicate freely with HTTP, regardless of the platform and language of the two programs.

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

WebService uses the HTTP protocol to transmit data and encapsulates data in XML format, that is, the method used to call the remote service object, the parameters passed, and the returned results of the service object are described in XML. XML is the format of data in the WebService platform. Apart from being easy to establish and analyze, XML is mainly because it is platform-independent and vendor-independent.

When SOAP and WebService send requests and receive results over HTTP, the content and results of the sent requests are both encapsulated in XML format, and some specific HTTP message headers are added, to describe the content format of HTTP messages, these specific HTTP message headers and XML content formats are the SOAP protocol. SOAP provides a standard RPC method to call Web Services. SOAP protocol = HTTP protocol + XML data format.

Web Service Description Language (WSDL) is an XML-based Language used to describe Web services and their functions, parameters, and returned values. It is a standard format that can be understood by both the WebService client and the server. Because it is based on XML, the WSDL can be read by machines in a timely manner and is readable by humans. The WSDL file is stored on the Web server and can be accessed through a url. Before the client calls a WebService, it must know the address of the Service's WSDL file. WebService providers can expose their WSDL file addresses in two ways: 1. Register with the UUID server to be searched; 2. directly inform the client caller.

2) Apache configuration

In order to implement Web services, httpd must support soap extensions. Here we take the yum installation of httpd and soap extensions as an example to describe:

1> install httpd

Yum install httpd

2> install php

Yum install php

3> Configure httpd to support php Parsing

Add the following to/etc/httpd/conf/httpd. conf:

AddType application/x-httpd-php. php

AddType application/x-httpd-php-source. phps

4> restart httpd

Service httpd restart

5> check whether the soap extension is installed

/Usr/bin/php-m | grep soap //! If yes, the soap extension is installed. If no, perform the next step.

6> install soap Extension

Yum install php-soap //! Check again after installation and restart httpd

7> verify that the soap extension is correctly installed

You can use php-m | grep soap or phpinfo.

3) Implement WebService server in PHP

1> define a Service-Service. php

This server is the interface you want to provide externally. Suppose we need to provide a function externally, which receives a string and then returns the "Hello" + input string. The service class is very simple, as follows:

<?php   class Service   {public function sayHello($content){      return "Hello ".$content;}   }?>

2> Generate the wsdl file -- create_wsdl.php

To generate the wsdl file, we can use the SoapDiscovery provided on the Internet. class. the method provided by getWSDL in php. This SoapDiscovery. class. php code can be downloaded online. To generate a file, we need to modify some code in the getWSDL method (with the code for writing the file), as shown below:

// Return sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions> ');//! Comment out return and add the following code to call getWSDL for generation. wsdl File $ fso = fopen ($ this-> class_name. ". wsdl "," w "); fwrite ($ fso, sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions>'); fclose ($ fso );

Now we can define our own create_wsdl.php. Just call this method, as shown below:

<? Php include ("Service. php"); include ("SoapDiscovery. class. php ");//! The first parameter is the class name, And the generated wsdl file is named after it. The second parameter is the Service name. You can write $ disco = new SoapDiscovery ('service ', 'soap '); $ disco-> getWSDL ();?>

3> modify server -- Service. php

Modify Service. php and add Code as follows:

<? 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 ");//! Register all methods of the Service class $ service-> handle ();//! Request Processing?>

4> modify the Service. wsdl File

5> client test-client. php

Client. php is used to test whether the Server of the Web Server we just wrote is available. The Code is as follows:

<?php   $soap = new SoapClient('http://192.168.17.129/soap/Service.php?wsdl');     echo $soap->sayHello("houqd2012");?>

The output result is as follows:

Hello houqd2012

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.