PHP Write WebService service side

Source: Internet
Author: User
Tags soap php write sprintf wsdl

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 the server are free to communicate with HTTP , regardless of the platform of two programs and what language is.

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

WebService transmits data using the HTTP Protocol, encapsulating the data in XML format, XML Describes which method of invoking the remote service object, what parameters are passed, and what the return result of the service object is. XML is a format for representing data in the WebService platform, in addition to being easy to establish and easy to parse,XML The main thing is that it is both platform-independent and vendor-independent.

Soap,WebServicethroughHTTPwhen the protocol sends the request and receives the result, the requested content and the result content are sent usingXMLformat, and adds a number of specificHTTPThe message header to illustrateHTTPThe content format of the message, these specificHTTPmessage headers andXMLThe content format isSOAPAgreement,SOAPprovides a standardRPCmethod to invoke theWeb Service. SOAPProtocol= HTTPProtocol+ XMLdata format.

Wsdl(Web Service Description Language) is based onXMLthe language used to describeWeb Serviceand its functions, parameters, and return values, which areWebServicea standard format that both the client and server sides can understand. Because it is based onXMLof, soWSDLtimely machine readable, but also people can read. WSDLfiles are saved inWebserver, through aURLaddress, you can access it. The client wants to invoke aWebServiceservice, be aware of the service'sWSDLthe address of the file. WebServiceThere are two ways that a service provider can expose itsWSDLfile Address:1, register toUUIDservers so that they can be looked up;2, directly to the client caller.

2) ApacheRelated Configuration

In order to be able to implement a Web Service,httpd must support soap extensions, where yum installs examples of httpd and soap extensions are:

1> installation httpd

Yum Install httpd

2> installing php

Yum Install PHP

3> configuration httpd support php parsing

Add the following in /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 if the soap extension is installed

/usr/bin/php-m | grep soap//! if it exists, it means that it has been installed Soap extension, if not present, perform the next step

6> Installing the soap extension

Yum Install Php-soap//! Check again after installation, restart httpd

7> Verifying that the soap extension is installed correctly

This can be done either through php-m | grep soap , or through the phpinfo ()method.

3) PHPImplementWebServiceService Side

1> Definition Service class--service.php

The service side is to implement the interface you want to provide externally, suppose we need to provide a function, it receives a string, and then return "Hello" + input string This small function, the service class is very simple, as follows:

<?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 can use the getwsdl in the SoapDiscovery.class.php provided online Provided by the method to implement, the SoapDiscovery.class.php code can be downloaded to the Web, in order to generate files, we need to modify getwsdl Part of the code in the method (plus the code that writes the file), as follows:

return sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ' </ Definitions> ');//! comment out return, add the following code in order to call GETWSDL Generate. 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, as follows:

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

3> Modify Server--service.php

Modify the service.phpand add some 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"); //! All methods of registering the service class   $service->handle ();//! processing Request?>

4> modifying service.wsdl files

5> Client Test--client.php

Client.php is used to test the availability of the Web Server that we just wrote , with 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.