PHP Learning: Using instances of soap in PHP and generating WSDL files, providing class libraries that automatically generate WSDL files--soapdiscovery.class.php class

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

1. Web Service Popularity : Webservice the difference between soap WSDL and personal insights

web service to achieve business demand:  web service is really" the one ", Provides a general designation of a service interface.
wsdl provides "documentation of what can be done description ":   A descriptive format for the service to be provided. I want to help you, but I'm going to tell you what I can do, and what kind of parameters I need to do these things.
soap provides spec :   Pass the requested format to the service interface, including methods and parameters. You want to let people do things, you have to tell people what you want to do, soap is the format of the definition of this "request", according to the "request" format of the SOAP definition "write" the request can ensure that the Web service can correctly interpret what you want it to do and what parameters you provide for it. The main issues you need to describe in this request are: which Web service to send the request to, the parameter type of the request, the parameter value, the return value type. These are all "filled in" and a SOAP message conforming to the SOAP specification is completed.

While WSDL and soap are two of the standards of Web service, they are not necessarily connected and can be used independently.
WSDL provides a unified interface that has now become an internationally recognized standard, with interfaces provided by WSDL that can access different types of resources (such as Java, C #, C, C, C + +, and so on), because WSDL is XML-based and language platform agnostic. In addition, the WSDL provides binding and service elements to bind the interface to the specific service, realizing the separation between the interface and the implementation.

Soap (Simple Object Access Protocol) is an HTTP-based transport protocol used to access remote services

the relationship between WSDL and soap is: The WSDL binding service can be set to use the Protocol, the Protocol can be SOAP, HTTP, SMTP, FTP and so on any kind of transport protocol, in addition to WSDL can also bind JMS, EJB and local Java and so on , but it is necessary to extend the binding and service elements, and to extend the capabilities of the server to support this extension

The SOAP protocol is a request and response protocol specification, and HTTP is the Web transport protocol, and SOAP transfers can be based on HTTP, but can also be based on other transport protocols such as FTP, SMTP, and so on.

Simple Object Access Protocol (SOAP) is a note from the World Trade Organization, which describes a lightweight protocol for exchanging information in a decentralized or distributed environment. SOAP is an XML-based protocol that consists of three parts: the soap wrapper (Envelop), which defines a framework that describes what the content in the message is, who sent it, who should accept and handle it, and how to handle it; SOAP encoding Rules (Encoding rules), An instance that represents the data type that the application needs to use, a SOAP RPC representation (RPC representation), a contract that represents a remote procedure call and an answer, and a soap that can be bound to a variety of transport protocols (binding), exchanging information using the underlying protocol. In this document, only the framework for how SOAP is bound to HTTP and HTTP extensions is currently defined.

SOAP is a communication protocol, SOAP on the basis of the HTTP protocol, the request parameters written as XML, placed on the HTTP body on the submission of a Web service server (servlet,asp, etc.) processing is completed, The result is also written as XML as response back to the client, in order to make the client and the Web service can correspond to each other, you can use WSDL as a description of this mode of communication, using the WSDL tool can automatically generate WS and client framework files, SOAP has the ability to serialize complex objects into XML.

SOAP is the predecessor of RPC, is the protocol of Remote Call processing, this Protocol security is not very good, most firewalls will block RPC communication packets, while SOAP uses the HTTP protocol as the basic protocol, using port 80 allows soap to complete the RPC function through the firewall.

The SOAP protocol, like the HTTP protocol, is the underlying communication protocol, but the format of the request package is different, the SOAP packet is in XML format, and now we do not need to understand soap in depth to write a Web service. If the service and client are using SOAP in the same environment, there is usually a tool for automatically generating the SOAP program framework, so it's okay not to know the details. However, if the client and service environment are different, such as Java's client and. NET service to communicate, or VB client and Tomcat under the Java Service communication, or to know a little bit of detail as well. In particular, WSDL or UDDI is not a standard, but if not used, you have to manually formulate a SOAP message.

2. Use the class "SoapDiscovery.class.php" to generate the WSDL document!

interface of SOAP Type
Irrelevant programming language, irrelevant platform, extensibility is good

To implement a SOAP-type interface, there are two ways: a WSDL file, a non-WSDL file mode!
For people who love research, the first way is to give you a clear idea of how PHP created a Web service! But the first one for beginners, it is difficult to create a WSDL file in XML format, which you first understand what is xml! Learn XML Syntax! But for a person who is eager to solve the problem! Not so much time to get acquainted! So this is a matter of annoyance! But no hurry, it says, there is a way to not need a WSDL file! Also, this tutorial provides a class that automatically generates WSDL files!

Before you explain, configure the SOAP environment support for PHP:
Locate php.ini File
; Extension=php_soap.dll
Delete ";", restart Apache server

first, there is a WSDL file mode
Here we first introduce the standard webservice. So how do you create WSDL? This is a really hard thing for PHP, and some people say it's easy to create it with Zend Studio, which is a method. But for those of you who don't like using Zend Studio, it's too much to think of creating a Web service and installing Zend Studio.
Here is a simple way to download the SoapDiscovery.class.php class online (I have a local download), there is aCommon methods: getwsdl (), this method ends with a return statement, so you modify this method:
return sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ' </ Definitions> ');
Generate the WSDL file and remove the return comment above, then
$fso = fopen ($this->class_name. ". wsdl", "w");
Fwrite ($fso, sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ' </ Definitions> '));
Now the class that generated the WSDL has, SoapDiscovery.class.php (source at the very end).
You can create a WSDL by preparing a service.php class file or function that provides the service!
<?php
Class Service {

Public Function HelloWorld () {
return "Hello";
}

Public function Add ($a, $b) {
return $a + $b;
}

}
?>

The following creates a WSDL file for the creat_wsdl.php
<?php
Include ("service.php");
Include ("SoapDiscovery.class.php");

$disc = new Soapdiscovery (' Service ', ' soap '); The first parameter is the class name (the generated WSDL file is named after it, for example: SERVICE.WSDL below), which is the service class, and the second parameter is the name of the services (this can be casually written)
$disc->getwsdl ();
# #说明: By viewing the source discovery of the "SoapDiscovery.class.php" file, this member function returns an XML-formatted string if the return statement is opened, if $strXML = $disc->getwsdl (); echo $strXML; The WSDL file for the XML will not be displayed in the browser. Because the browser defaults to "Html/text" to return the contents of stdout, it needs to be used in the header of the PHP script:
header ("content-type:text/html; Charset=utf-8 "); Can let the browser not to the output as HTML but as XML to parse!
Run the create_wsdl.php file as a Web access, you will generate aSERVICE.WSDL's File, this file will be used here!
Note: The WSDL file generated by the above method is no problem under Windows, but the "Location=xxx" segment in the WSDL file under Linux does not have a server IP address. So toModify the code in: $_server[' server_name '] for $_server[' server_host '], because under Linux (Nginx), the former variable is empty so that the correct WSDL document can be generated!


and add some code to the service.php file.
<?php
Class Service {

Public Function HelloWorld () {
return "Hello";
}

Public function Add ($a, $b) {
return $a + $b;
}

}

$server = new soapserver('Service. wsdl ', array (' soap_version ' = soap_1_2)); # #此处的 The service.wsdl file is generated above
$server-SetClass("Service"); Registering all methods of the service class
$server->handle (); Processing requests
?>

CreateWebService ClientProgram to test whether the webservice is valid and the file name is: client.php
Copy in the following:
<?php
Ini_set (' soap.wsdl_cache_enabled ', "0"); Turn off the WSDL cache
$soap = new SoapClient (' http://localhost/Dragon/soap/Service.php?wsdl ');
Echo $soap->add (28, 2);
echo $soap->__soapcall (' Add ', Array (28,2))//or so called
#echo $soap->__call (' Add ', Array (28,2));

?>
Ok! Test pass!

  

Two, no WSDL file mode
Server-side
<?php
Class Service
{
Public Function HelloWorld ()
{
return "Hello";
}
Public function Add ($a, $b)
{
return $a + $b;
}
}
$server =new soapserver (null,array (' uri ' = ' abcd '));
$server->setclass ("Service");
$server->handle ();
?>
Client
<?php
try {
$soap = new SoapClient (NULL, Array (
"Location" = "http://localhost/Dragon/soap/Service.php",
"uri" = "ABCD",//Resource descriptor server and client must correspond to
"Style" = Soap_rpc,
"Use" = soap_encoded
));

echo $soap->add (12, 2);
} catch (Exction $e) {
echo Print_r ($e GetMessage (), true);
}
?>

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.