PHP soap Web Service uses SoapDiscovery. class. php to generate the wsdl file,

Source: Internet
Author: User

PHP soap Web Service uses SoapDiscovery. class. php to generate the wsdl file,

PHP soap web service uses the wsdl file demo:

========================================================== ======================================

Server:

Use the cw. php file to generate the wsdl file before using the soap webservice demo of wsdl. The Code is as follows:

Cw. php:

<? Phpinclude ("service. php "); include (" SoapDiscovery. class. php "); $ disco = new SoapDiscovery ('soaphandle', 'myservice'); // The first parameter is the class name (the generated wsdl file is named after it ), that is, the Service class. The second parameter is the Service name (which can be written as needed ). $ Disco-> getWSDL ();

?>

 

Service. php:

<? Phpinclude_once ("soapHandle. class. php "); $ server = new SoapServer ('soaphandle. wsdl ', array ('soap _ version' => SOAP_1_2); # the Service here. the wsdl file is the $ server-> setClass ("soapHandle") generated above; // all methods for registering the Service class $ server-> handle (); // process the request?>

 

SoapHandle. class. php:

<?phpclass soapHandle{    public function strtolink($url=''){        return sprintf('<a href="%s">%s</a>', $url, $url);    }        public function add($a=1, $b=2)    {        return $a+$b;    }}?>

 

The SoapDiscovery. class. php code is as follows:

========================================================== ==============================

<? Php/*** Copyright (c) 2005, Braulio Jos? Solano Rojas * All rights reserved. ** Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: ** Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclawing. * Redistributions in binary form must reproduce the above copyright notice, this list Of * conditions and the following disclawing in the documentation and/or other materials * provided with the distribution. * Neither the name of the Solsoft de Costa Rica S. a. nor the names of its contributors may * be used to endorse or promote products derived from this software without specific * prior written permission. ** this software is provided by the copyright holders and * CONTRIBUTOR S "as is" and any express or implied warranties, * INCLUDING, but not limited to, the implied warranties of * merchantability and fitness for a particle purpose are * DISCLAIMED. in no event shall the copyright owner or * contributors be liable for any direct, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, or consequential damages (INCLUDING, BUT * not limited, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * loss of use, DATA, or profits; or business interruption) * however caused and on any theory of liability, whether in * CONTRACT, strict liability, or tort (including negligence or * OTHERWISE) arising in any way out of the use of this software, * even if advised of the possibility of such damage. * ** @ version $ Id $ * @ copyright 2005 * // *** SoapDiscovery Class that provides Web Servi Ce Definition Language (WSDL). ** @ package SoapDiscovery * @ author Braulio Jos? Solano Rojas * @ copyright Copyright (c) 2005 Braulio Jos? Solano Rojas * @ version $ Id $ * @ access public **/class SoapDiscovery {private $ class_name = ''; private $ service_name ='';/*** SoapDiscovery :: __construct () SoapDiscovery class Constructor. ** @ param string $ class_name * @ param string $ service_name **/public function _ construct ($ class_name = '', $ service_name = '') {$ this-> class_name = $ class_name; $ this-> service_name = $ service_name ;}/** * SoapDiscovery: getWSDL () Returns the WSDL of a class if the class is instantiable. ** @ return string **/public function getWSDL () {if (empty ($ this-> service_name) {throw new Exception ('no service name. ') ;}$ headerWSDL = "<? Xml version = \ "1.0 \"?> \ N "; $ headerWSDL. = "<definitions name = \" $ this-> service_name \ "targetNamespace = \" urn: $ this-> service_name \ "xmlns: wsdl = \" http://schemas.xmlsoap.org/wsdl/\ "xmlns: soap = \ "canonical" xmlns: tns = \ "urn: $ this-> service_name \" xmlns: xsd = \ "http://www.w3.org/2001/XMLSchema\" xmlns: SOAP-ENC = \ "http://schemas.xmlsoap.org/soap/encoding/\" xmlns = \ "http://schemas.xmlsoap.org/wsdl/\"> \ n "; $ HeaderWSDL. = "<types xmlns = \" http://schemas.xmlsoap.org/wsdl/\ "/> \ n"; if (empty ($ this-> class_name) {throw new Exception ('no class name. ');} $ class = new ReflectionClass ($ this-> class_name); if (! $ Class-> isInstantiable () {throw new Exception ('class is not instantiable. ') ;}$ methods = $ class-> getMethods (); $ portTypeWSDL =' <portType name = "'. $ this-> service_name. 'port "> '; $ bindingWSDL =' <binding name = "'. $ this-> service_name. 'binding "type =" tns :'. $ this-> service_name. "Port \"> \ n <soap: binding style = \ "rpc \" transport = \ "http://schemas.xmlsoap.org/soap/http\"/> \ n "; $ serviceWSDL = '<service name =" '. $ This-> service_name. "\"> \ n <documentation/> \ n <port name = \"". $ this-> service_name. 'port "binding =" tns :'. $ this-> service_name. "Binding \"> <soap: address location = \ "http ://". $ _ SERVER ['server _ name']. ':'. $ _ SERVER ['server _ port']. $ _ SERVER ['php _ SELF ']. "\"/> \ n </port> \ n </service> \ n "; $ messageWSDL =''; foreach ($ methods as $ method) {if ($ method-> isPublic ()&&! $ Method-> isConstructor () {$ portTypeWSDL. = '<operation name = "'. $ method-> getName (). "\"> \ n ". '<input message = "tns :'. $ method-> getName (). "Request \"/> \ n <output message = \ "tns :". $ method-> getName (). "Response \"/> \ n </operation> \ n "; $ bindingWSDL. = '<operation name = "'. $ method-> getName (). "\"> \ n ". '<soap: operation soapAction = "urn :'. $ this-> service_name. '#'. $ this-> class_name. '#'. $ method-> getName (). "\"/> \ n <input> <Soap: body use = \ "encoded \" namespace = \ "urn: $ this-> service_name \ "encodingStyle = \" http://schemas.xmlsoap.org/soap/encoding/\ "/> \ n </input> \ n <output> \ n <soap: body use = \ "encoded \" namespace = \ "urn: $ this-> service_name \ "encodingStyle = \" http://schemas.xmlsoap.org/soap/encoding/\ "/> \ n </output> \ n </operation> \ n"; $ messageWSDL. = '<message name = "'. $ method-> getName (). "Request \"> \ n "; $ parameters = $ method-> get Parameters (); foreach ($ parameters as $ parameter) {$ messageWSDL. = '<part name = "'. $ parameter-> getName (). "\" type = \ "xsd: string \"/> \ n ";}$ messageWSDL. = "</message> \ n"; $ messageWSDL. = '<message name = "'. $ method-> getName (). "Response \"> \ n "; $ messageWSDL. = '<part name = "'. $ method-> getName (). "\" type = \ "xsd: string \"/> \ n "; $ messageWSDL. = "</message> \ n" ;}}$ portTypeWSDL. = "</portType> \ n"; $ bindingWSDL. = "</Binding> \ n"; // return sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions>'); $ fso = fopen ($ this-> class_name. ". wsdl "," w "); fwrite ($ fso, sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions>');}/*** SoapDiscovery: getDiscovery () Returns discovery of WSDL. ** @ return string **/ Public function getDiscovery () {return "<? Xml version = \ "1.0 \"?> \ N <disco: discovery xmlns: disco = \ "http://schemas.xmlsoap.org/disco/\" xmlns: scl = \ "timeout"> \ n <scl: contractRef ref = \ "http ://". $ _ SERVER ['server _ name']. ':'. $ _ SERVER ['server _ port']. $ _ SERVER ['php _ SELF ']. "? Wsdl \ "/>\n </disco: discovery>" ;}}?>View Code

 

Client:

Client. php code:

========================================================== ==================================

<? Php ini_set ('soap. wsdl_cache_enabled ', "0"); // disable the wsdl cache $ soap = new SoapClient ('HTTP: // localhost/soap/service. php? Wsdl '); echo $ soap-> strtolink ('HTTP: // www.baidu.com '). "<br/>"; echo $ soap-> add (28,100 ). "<br/>"; echo $ soap->__ soapCall ('add', array (28,200 )). "<br/>"; // or Call echo $ soap->__ Call ('add', array (28,300 )). "<br/>"; echo date ('Y-m-d H: I: s', time ();?>

 

====================== END ===============================

 

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.