About the pitfall of php calling. net web service,. netweb

Source: Internet
Author: User

About the pitfall of php calling. net web service,. netweb

From the past, the company had to connect to the other party as a web service. Because the other party uses the. net language, we use php. The manager originally asked us to use. net to write the web service server. I forgot all the. net files I learned at school...

As a result, I began to use php for programming for online search.

Modify php. ini; extension = php_soap.dll

First, write a simple server test method.

class Service {    public function HelloWorld() {        return "Hello";    }}

Save this method as a PHP file, and a web service file will be ready.

Because web service requires a wsdl file in xml format to parse the method. Therefore, we also need a wsdl file. net is more convenient to automatically generate, and php has to write it on our own. However, if there is already a wheel on the Internet, we just need to call it directly.

<? 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 = '<se Rvice 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. '#'. $ met Hod-> 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 (). "Reque St \ "> \ n"; $ parameters = $ method-> getParameters (); 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>'); // generates the wsdl file and comments the preceding return $ fso = fopen ($ this-> class_name. ". wsdl "," w "); fwrite ($ fso, sprintf ('% s % s', $ headerWSDL, $ portTypeWSDL, $ bindingWSDL, $ serviceWSDL, $ messageWSDL, '</definitions>');}/*** SoapDis Covery: 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>" ;}}?>
SoapDiscovery. class. php

Save the above Code as a PHP file as the manufacturing method of wsdl.

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

Finally, run the above Code to generate a wsdl file. However, the second parameter in new SoapDiscovery should be noted. This parameter is the service name of the service, which cannot be used when php is called, but if your web service is written. net, the name should be agreed with the other party. Because. net uses this name as the class name to call our method.

Now, the wsdl file is generated. At this time, we need to add a piece of code to the service method we first wrote.

<? Phpclass Service {public function HelloWorld () {return "Hello" ;}$ server = new SoapServer ('service. wsdl ', array ('soap _ version' => SOAP_1_2); $ server-> setClass ("Service "); // register all methods of the Service class $ server-> handle (); // process the request?>

As shown above. Here, the web service server is complete. Is the address of the Server File http://xxxxx.Service.php? Wsdl. You can send this address to someone else.

Next, create a client file. Here I don't need to write the method just now, with a weather forecast on the Internet web servcie http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

<? Phpheader ("Content-Type: text/html; charset = UTF-8"); $ client = new SoapClient ('HTTP: // www.webxml.com.cn/WebServices/WeatherWebService.asmx? Wsdl '); var_dump ($ client->__ getFunctions (); // print out all the web servcie Methods $ aaa = $ client-> getsuppdatasdataset (); // call one of the methods without parameters var_dump ($ aaa); // The output return value print_r ($ aaa); // The output return value?>

Write a client file as above. In this case, I am calling. net method, stuck for a long time, and finally found that the parameter format of the other side is a problem, here special mention, pay attention to the parameter name in the other side of the wsdl file. If the "php [soap: Server] Server cannot process requests in normal mode. ---> When the root element is missing, pay attention to the parameter name in the target wsdl file. I used $ aaa = $ client->__ Call ("ApplyStateConfirm", array ($ str). This method is successful. Note that $ str is also an array, the key name is the parameter name specified in the target wsdl file.

Such a web service is successfully called.

Finally, if you find that some methods do not work during debugging, it is caused by the wsdl cache of php soap. Disable it in the php. ini file and restart nginx.

The solution is to modify soap. wsdl_cache_enabled = 1 (changed to 0) and soap. wsdl_cache_ttl = 86400 (changed to 0) in php. ini. Note that both the client and server must be modified.

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.