A probe into PHP webservice

Source: Internet
Author: User
Tags soap wsdl

background : In the recent development, in order to solve the internal system and external system docking, began to contact the WebService interface, external companies provide interface for us to call, has reached the purpose of data synchronization, it is necessary to popularize the knowledge of Web service!

What is Web Service: Web Service is a platform-independent, low-coupling, self-contained, programmable Web-based application that can be used to describe, publish, discover, and use Open XML (a subset of standard generic tags under the park) standard Orchestrate and configure these applications for the development of distributed, interoperable applications.

WebService three elements : SOAP, WSDL (WebServicesDescriptionLanguage), UDDI (U niversal Descriptiondiscoveryand Integration), soap is used to describe the format of the passing of information, WSDL is used to describe how to access the specific interface, UDDI is used to manage, distribute, and query webservice. The concrete implementation can search the Web services simple instance ; SOAP can be used in conjunction with many existing Internet protocols and formats, including Hypertext Transfer Protocol (HTTP), Simple Mail Transfer Protocol (SMTP), Multipurpose Internet Mail Expansion Protocol (MIME). It also supports a large number of applications, from the messaging system to Remote procedure calls (RPCs). SOAP uses a combination of XML-based data structures and Hypertext Transfer Protocol (HTTP) to define a standard method for using distributed objects in various operating environments on the Internet.

What is soap: Soap is a simple XML-based protocol that enables an application to exchange information over HTTP.

    • SOAP refers to the Simple Object Access Protocol
    • SOAP is a communication protocol
    • SOAP is used for communication between applications
    • SOAP is a format for sending messages
    • SOAP is designed to communicate over the Internet
    • SOAP independent of platform
    • SOAP independent of language
    • SOAP based XML
    • SOAP is simple and extensible
    • SOAP allows you to bypass the firewall
    • SOAP will be developed as a standard .

http vs. soap:

    • An easy-to-understand statement (HTTP is the Post Office Protocol, they specify how you want your envelopes to be written, how many stamps to post, and so on, soap is an agreement between you, responsible for writing what you need to say on stationery, and being responsible for allowing the other person to read your letter. );
    • In fact, HTTP is the most common transport tool for SOAP messages. When soap serializes the information into XML, it is then packaged in the same way as the HTTP protocol, and it is transmitted in either TCP or UDP. A metaphor is a good idea. Both TCP and UDP are highways, and for the moment TCP is considered a general highway, UDP Highway, SOAP and HTTP are all cars, then both soap and HTTP can run on TCP and UDP. Said soap can be transmitted via HTTP, which is actually said that soap is a car, HTTP is a car truck, the SOAP information into the HTTP, and then transport, of course, the road is still TCP or UDP. Said soap can be transmitted through the HTTP protocol, this sentence is not very accurate, more accurate: The SOAP message can be wrapped through the HTTP protocol via TCP or UDP transmission.

what is WSDL: WSDL (Network Service Description Language, Web Services Description Language) is an XML-based language that describes Web services and how to access them.

    • WSDL refers to the Network Service Description Language
    • WSDL is written using XML
    • WSDL is an XML document
    • WSDL is used to describe network services
    • WSDL can also be used to locate network services
    • WSDL is not a standard

what is UDDI: UDDI is a directory service that enterprises can use to register and search for WEB services.

    • UDDI refers to general description, discovery, and Integration Services
    • UDDI is a directory for storing information about Web services.
    • UDDI is a directory of Web services interfaces that are described by WSDL.
    • UDDI communicates through SOAP
    • UDDI was built into Microsoft's. NET Platform

What UDDI is based on:

    • UDDI uses Internet standards such as XML, HTTP, and DNS for ietf*.
    • UDDI uses WSDL to describe the interface that arrives at Web services
    • In addition, through the adoption of soap, you can also achieve cross-platform programming features, you know, soap is an XML protocol communication specification, can be found on the web site of the relevant information.

PHP Create WebService:

Premise: The environment should ensure that PHP supports soap;

Create a. wsdl file (mode: 1, directly generated using the Zend Studio tool, 2, automatically generate a WSDL file using SoapDiscovery.class.php)

    • Define the service class: service.php, the server is to implement the interface provided externally, such as Write a Class (Person.class.php); code is as follows:
<?php/** * Created by Phpstorm. * User: Sergeant * DATE:2018/6/19 * time:11:40 */class person{public    function say ()    {        return ' I'm talking. ";    }    Public function run ()    {        Return "I'm Running";    }}
    • Create the. wsdl file with the service.php file, and the code is as follows:
<?php/** * Created by Phpstorm. * User: Sergeant * DATE:2018/6/19 * time:11:41 */include ("Person.class.php"); Include ("SoapDiscovery.class.php");// The first parameter is the class name (the generated WSDL file is named after it), the person class, and the second parameter is the name of the service (this can be written casually). $disco = new Soapdiscovery (' person ', ' person '), $disco->getwsdl ();
    • Browser directly accesses service.php, generates PERSON.WSDL file
<?xml version= "1.0"? ><definitions name= "Person" targetnamespace= "Urn:person" xmlns:wsdl= "/http             schemas.xmlsoap.org/wsdl/"xmlns:soap=" http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns=" Urn:person " Xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" xmlns:soap-enc= "http://schemas.xmlsoap.org/soap/encoding/" xml ns= "http://schemas.xmlsoap.org/wsdl/" > <types xmlns= "http://schemas.xmlsoap.org/wsdl/"/> <porttype Name= "Personport" > <operation name= "Say" > <input message= "tns:sayrequest"/> &L  T;output message= "Tns:sayresponse"/> </operation> <operation name= "Run" > <input Message= "Tns:runrequest"/> <output message= "Tns:runresponse"/> </operation> </port type> <binding name= "personbinding" type= "Tns:personport" > <soap:binding style= "rpc" transport= "http       ://schemas.xmlsoap.org/soap/http "/> <operation name= "Say" > <soap:operation soapaction= "Urn:person#person#say"/> &LT;INPUT&G                T <soap:body use= "encoded" namespace= "Urn:person" encodingstyle= "Http://schemas.xmlsoap.org/soa p/encoding/"/> </input> <output> <soap:body use=" encoded "namespace = "Urn:person" encodingstyle= "http://schemas.xmlsoap.org/soap/encoding/"/> </out put> </operation> <operation name= "Run" > <soap:operation soapaction= "Urn:person                           #Person #run "/> <input> <soap:body use=" encoded "namespace=" Urn:person " encodingstyle= "http://schemas.xmlsoap.org/soap/encoding/"/> </input> <outpu T> <soap:body use= "encoded" namespace= "Urn:person" encodingstyle= "http://sc Hemas.xmlsoap.org/soap/encoding/"/> </output> </operation> </binding> <service N            Ame= "Person" > <documentation/> <port name= "Personport" binding= "tns:personbinding" > <soap:address location= "http://localhost:80/wsdl/Service.php"/> </port> </service> <me Ssage name= "Sayrequest" > </message> <message name= "Sayresponse" > <part name= "Say" type= "xsd : string "/> </message> <message name=" Runrequest "> </message> <message name=" runrespons E "> <part name=" Run "type=" xsd:string "/> </message></definitions>
    • Empty the contents of the service.php file and copy the code in:
<?php/** * Created by Phpstorm. * User: Sergeant * DATE:2018/6/19 * time:11:41 */include ("Person.class.php"); $objSoapServer = new SoapServer ("person.wsdl"); /PERSON.WSDL is the WSDL file that you just created//$objSoapServer = new SoapServer ("server.php?wsdl");//This line $objsoapserver->setclass (" Person ");//Register all methods of the person class $objsoapserver->handle ();//Processing requests
    • Create the WebService client program, test the WebService whether the mailbox, the file name is: client.php, the code is as follows:

Summary:. NET if you want to use, just provide a URL to him; Get URL method: You can find <soap:address location= in the person.wsdl file first "http://localhost:80/wsdl/ Service.php "/>, here the URL (the specific URL is determined by your directory) is what you want to provide. NET developers, but don't be happy too early, add the following: "? wsdl", http://localhost:80/wsdl/Service.php?wsdl this is the right, do not believe you can copy the URL to the browser's address bar to see that. NET developer gets the URL you gave him, you can add a service reference or Web reference to your project, and then you can follow the prompts to complete the operation, for use. NET developers are simple.

A probe into PHP webservice

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.