PHP SOAP EXTENSION

xiaoxiao2021-03-06  104

PHP SOAP EXTENSION

By Dmitry Stogov March 16, 2004

Body, Center, TD, P, LI {Font-Family: Verdana, Arial, Helvetica, Sans-Serif; Font-size: 11px; Color: # 333333}

UL {margin-left: 20;}

H2 {Font-Family: Arial, Helvetica, Sans-Serif; Font-size: 16px; Color: # 336699}

H3 {Font-Family: Arial, Helvetica, Sans-Serif; Font-size: 14px; Color: # 333333}

Intended AudienceIntroductionA First SOAP Client • Example 1 (client1.php) • Example 2 (client2.php) • A First SOAP Server • Example 3 (stockquote.wsdl) • Example 4 (server1. Php) • Example 5 (client3.php) • Example 6 (server2.php) • Example 7 (client4.php) What's inside? • Example 8 (client5.php) Other Implementations of SOAP for PHPSummaryReferencesAbout the Author Intended Audience This article describes the new SOAP extension for PHP. It is intended for PHP developers who want to write their own Web Services servers, or use SOAP to access existing ones. It assumes some familiarity with Web Services, SOAP, and WSDL (Web Services Description Language). Introduction SOAP (Simple Object Access Protocol) is a lightweight XML-based protocol for exchanging structured information between distributed applications over native web protocols, such as HTTP. SOAP specifies the formats that XML messages should use, the way in which they should be processed, a set of encoding rules for standard and application-defined data types, and a convention for representing remote procedure calls and responses. Web Services is a modern and very popular technology. The list of protocols and technologies related to Web Services grows every day, but SOAP is probably the most important. It is rapidly becoming the standard protocol for accessing Web Services. It uses XML messages to exchange information across endpoints, and provides several advantages over other binary protocols. RPC (Remote Procedure Calls) support was originally a minor element in the design of SOAP, but this Feature is one of the most useful it has ketay. PHP 5 '

s SOAP extension is the first attempt to implement the SOAP protocol for PHP in C. It has some advantages over the existing implementations written in PHP itself, the main one being speed. The extension is currently marked as experimental, but should gradually become more stable and reliable as time progresses. The SOAP extension implements a large subset of SOAP 1.1, SOAP 1.2 and WSDL 1.1 specifications. The key goal is to use the RPC feature of the SOAP protocol. WSDL is used where possible in order to make the implementation of Web Services more straightforward. A First SOAP Client to demonstrate how to make a simple SOAP Client, we'll take the XMethods demo service, "Delayed Stock Quote", as our target. Before we start to write any PHP code, we'll need to gather some information about this particular service: The method name The endpoint URL where the service is running The SOAPAction header value for the method The namespace URI for the method Input and output parameter names and t Ypes Happily, All this Information is Available on The Xmethods Web Site At http://www.xmethods.com/ in The Form of the Service's RPC Profile:

Method NamegetQuoteEndpoint URLhttp: //64.124.140.30: 9090 / soapSOAPActionurn: xmethods-delayed-quotes # getQuoteMethod Namespace URIurn:? Xmethods-delayed-quotesInput ParametersSymbolStringOutput ParametersResultfloat Example 1 (client1.php) "http://64.124.140.30:9090/soap", "URI" => "URN: XMethods-delayed-quotes", "style" => soap_rpc, "use" => soap_encoded))) PRINT ($ client -> __ call (/ * soap method name * / "getquote", / * parameters * / array (new soapparam (/ * parameter value * / "ibm", / * parameter name * / "symbol") ), / * Options * / array (/ * soap method namespace * / "uri" => "urn: xmethods-delayed-quotes", / * soapaction http header for soap method * / "soapaction" => "URN: XMethods -delayed-quotes # get Quote ".))" / N ");?> As you can see, this simple task required a lot of work Fortunately, Web Services can describe themselves to the client using WSDL, and generally they achieve this successfully The location of the. WSDL document for the XMethods "Delayed Stock Quote" service is given on the information page for that service at xmethods.com: http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl Here is the same PHP SOAP Client, Rewritten Using That WSDL Document. Now We Don '

t need to specify the endpoint URI, namespace, SOAPAction header, encoding style and parameter types All the information comes from the WSDL file Example 2 (client2.php) getquote (" IBM "));?> That's a little easier, isn't it? What Are the Problems with WSDL? The only argument against using it is that the client has to load the relevant WSDL document from the server before the RPC can be made, and this can take a significant amount of time in a Web environment. in order to speed things up, PHP's ext / soap uses a WSDL caching feature that can be controlled through setting the soap.wsdl_cache_enabled, soap.wsdl_cache_dir and soap.wsdl_cache_ttl configuration directives, either in your php.ini or by using ini_set () (see Example 4). by default , WSDL CACHING IS TURNED On and Caches WSDL Files for One Day. Here Is The Soap Section for php.ini with default VAL . Ues You can paste it into your php.ini [soap] soap.wsdl_cache_enabled = "1";. Enables or disables WSDL caching feature soap.wsdl_cache_dir = "/ tmp"; sets the directory name where SOAP extension will put cache files soap .wsdl_cache_ttl = "86400"; (Time to Live) Sets the number of second while cached file will be used; instead of Original One A First SOAP Server Let '

s try to write our own SOAP Web service that will do the same as the XMethods "Delayed Stock Quote" service. The first task is to create a WSDL document describing our service in a format that client requests will understand. This requires minor modifications to the original document taken from the Xmethods site, so we'll start by taking a close look at that file. The message section defines two messages. The first is getQuoteRequest, which is a request to relay the getQuote message and takes one string parameter called symbol. The other is getQuoteResponse, which is a response to the getQuote message, containing one float value, named Result. The portType section defines one operation, getQuote, which describes which of the messages listed in the message section will be used to transmit the Request and response. The Binding Section Defines How The Messages Must Be Transmitted and Encoded. Here It Tells US That We Well Be Sending An RPC Request Using SOAP ENCODING ACROSS HTTP. IT Also specifies namespace and value of the SOAPAction header for the getQuote method. Lastly, the service section defines the endpoint URL where the service is running. Example 3 (stockquote.wsdl)

Note:... The WSDL caching feature is on by default During the development of your WSDL file it should be turned off Now it's time to create our server First, we'll implement the getQuote () function, which will be accessed as a service function by incoming request messages from the web Next, we'll create a SoapServer object and connect it with the service function using SoapServer :: addFunction () method As you will see, the SoapServer () constructor has only one parameter..: The path of the wsdl document truth 4 (Server1. PHP) 98.42); Function getQuote ($ SMBOL) {Global $ quotes; return $ quotes [$ Symbol];} INI_SET ("soap.wsdl_cache_enabled", "0"); // Disabling WSDL Cache $ Server = New SOAPSERVER ("stockquote.wsdl"); $ server-> addfunction ("getquote"); $ server-> Handle ();?> The Soapserver Can Work without a wsdl document in Much The Same Way That The So Obvious Benefits To Be Had from Setting I t up in this way. Were you to do so, you should ensure that the return values ​​are special objects of the SoapParam and SoapVar classes (as in the first example). Here is a client for accessing our own SOAP server. Nothing has changed . from the previous example except the WSDL location It assumes that "stockquote.wsdl" is in the same directory as our SOAP server Example 5 (client3.php) getQuote ("IBM"));> What Are The Problem areas with our server and client? to start with, They don't handle errors. What happens when the server doesn '

? T recognize the requested symbol The SOAP protocol specifies a special format of messages for reporting errors - SoapFault To generate such messages the server should throw an exception using the SoapFault object The first parameter to the SoapFault () constructor is a fault code string.. , and the second is a fault description string. The client should be written in such a way as to catch SoapFault exceptions. Secondly, it would be better to encapsulate Web Service functionality in a PHP class. in this case we would not need to use global variables and add each SOAP method to the server individually; we could add an entire class, and all its methods would be accessible through SOAP Save stockquote.wsdl as stockquote2.wsdl, and alter the soap: address on line 43 to point. to server2.php A modified version of our SOAP server and client follows:. Example 6 (server2.php) 98.42); function getQuote ($ symbol) { IF (Isset -> Quotes [$ SMBOL]) {Return $ this-> quotes [$ symbol];} else {throw new soapfault ("Server", "Unknown Symbol '$ SMBOL');}}} $ server = new SOAPSERVER ("stockquote2.wsdl"); $ server-> setclass ("quoteService"); $ server-> handle ();?> As you can see, I have used the soapserver :: setClass () Method to Connect The SOAPSERVER Object with the quoteservice class. EXAMPLE 7 (client4.php) / n"; print ($ client-> getquote ("IBM ")); Echo" / n "; print ($ client-> getquote (" microsoft ")); ECHO" / N <

/ pre> / n ";} catch (SOAPFAULT $ EXCEPTION) {Echo $ exception;}?> What's inside? Are you curious about the soap message format, or hoping to debug a so, this section ... is for you The SoapClient () constructor accepts an associative array as its second parameter, as you already saw in the first example Various options can be passed through this associative array Here are just two:

trace - allows the client to store SOAP requests and responses (turned off by default) exceptions - allows the client to control the exception mechanism (turned on by default) Take a look at the following SOAP client example It is derived from example 5,. and shows precisely what is transmitted between the client and the server. In order to retrieve this information we use the SoapClient methods __getLastRequest () and __getLastResponse (). Example 8 (client5.php) 1, "exceptions" => 0)); $ client-> getquote ("IBM"); Print "

 / n"; print "request: / n". Htmlspecialchars ($ client -> __ getLastRequest ()). "/ n"; print "response: / n" .htmlspecialchars ($ client -> __ getLastResponse ()). "/ n"; print "";?>> IT is modified a little, to make it it more easily understand. Request:      IBM     Response: 

"1.0" encoding = "UTF-8"?> 98.42 Other importations of soap for phppear :: SOAP (http : //pear.php.net) NusoAP (http://dietrich.ganx4.com/nusoap) EZ SOAP (http://ez.no) All the Above Are Written in PHP, Rather Than in C. Summary in this . article I have described only the basic functionality of the SOAP extension for PHP in reality it can do significantly more, but it is not possible to demonstrate all its features in a short article The main ones are.:

转载请注明原文地址:https://www.9cbs.com/read-124793.html

New Post(0)