Você está na página 1de 6

PHP SOAP WSDL server, client and C# client ========================================== http://forum.codecall.net/csharp-tutorials/40281-c-soap-via-php-wsdl.

html ========================================================================= C# to Server Via PHP & SOAP *************************** When trying to learn SOAP & C# comunication I came across quite a few samples, but they were so overly complicated that you would get lost in what you wanted to learn. Remember KISS anyone? Well via this tutorial you will be able to create a SOAP (Simple Object Access Protocol) Server We will first build the PHP program that will process our functions that we create. It will be a simple Hello World type of program Create a File called hello_server.php, this will be saved on your Apache Web server running PHP 5.3.6 or better First we will create our Hello Function that will return "Hello " + whaterver string we give it. Code: -------------------------------------------------------------------------------<?php function HelloWho($Name) { return "Hello ".$Name; } -------------------------------------------------------------------------------That's the full Function, it takes $Name & Returns "Hello " + The Name, a simple PHP function Next to that file we create the SOAP Server itself witht the following lines of code Code: -------------------------------------------------------------------------------$server = new SoapServer("hello.wsdl"); $server->addFunction('HelloWho'); $server->handle(); ?> -------------------------------------------------------------------------------the line $server = new SoapServer("hello.wsdl"); points our SOAP Server to the wsdl file that will be used as an interface to the outside world. the line $server->addFunction('HelloWho'); adds our function we coded to the server allowing it to be called from the outside world.

and the last line starts our server. Not much to it so far. Full File Code: -------------------------------------------------------------------------------<?php function HelloWho($Name) { return "Hello ".$Name; } $server = new SoapServer("hello.wsdl"); $server->addFunction('HelloWho'); $server->handle(); ?> -------------------------------------------------------------------------------Next we are going to create the WSDL (Web Services Description Language) File this is the file that will allow other services to consume our functions (like Visual Studio for example) This wsdl file is avalable on my server, to run it on your own you will need to change a few lines. create a file called "hello.wsdl" (recall our hello_server.php points to this file, if you change the name, change it in the hello_server.php file also) First our headder these are the defaults you'll need to run this simple wsdl file. [code] <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" [code] next in the definitions you give it your root location of your server (the domain name you're hosting it at) Code: xmlns:tns="http://www.gw4y.com" targetNamespace="http://www.gw4y.com" and finaly a Name for the service this will become the CLASS in c# Code: name="HelloName" >

the Next section we'll be creating is in Message Definitions (Input output functions) Code: <message name="ServerResponse"> <part name="Return" type="xs:string" /> </message> <message name="NameInput"> <part name="FirstName" type="xs:string" /> </message> This defines "Return" and "FirstName" as a String. We will be using these to send the information to / from our server. next we create the Port (how our C# will comunicate with the PHP File) Code: <portType name="HelloClassPortType"> <operation name="HelloWho"> <documentation> This Is what actually talks to our hello_server.php function </documentation> <input message="tns:NameInput"/> <output message="tns:ServerResponse"/> </operation> </portType> the operation name="HelloWho" Must be the same name as in the server, yes you can "build" it if it's different, but when you try to actually run you'll get errors because the server can't find the command if the names don't match. the line <documentation> This Is what actually talks to our hello_server.php function </documentation> is optional, but allows you to know what the function will do. input message -> is what will go to the PHP function output message -> is what will be returned from the PHP function the next section binds our PHP server file to the wsdl file and to our classes. Code: <binding name="HelloClassBinding" type="tns:HelloClassPortType"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/htt p"/> <operation name="HelloWho"> <soap:operation/> <input> <soap:body namespace="urn:HelloService" encodingStyle="http://s chemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body namespace="urn:HelloService" encodingStyle="http://sc hemas.xmlsoap.org/soap/encoding/"/> </output> </operation> </binding> and finaly our actual service Code:

<service name="HelloName"> <documentation> This is the Hello Name Interface </documentation> <port name="HelloClass" binding="tns:HelloClassBinding"> <soap:address location="http://www.gw4y.com/helloworld/hello_server. php"/> </port> </service> </definitions> full file *********** WSDL ******************************** WSDL *************** Code: -------------------------------------------------------------------------------<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.gw4y.com" targetNamespace="http://www.gw4y.com" name="HelloName" > <message name="ServerResponse"> <part name="Return" type="xs:string" /> </message> <message name="NameInput"> <part name="FirstName" type="xs:string" /> </message> <portType name="HelloClassPortType"> <operation name="HelloWho"> <documentation> This Is what actually talks to our hello_server.php function </documentation> <input message="tns:NameInput"/> <output message="tns:ServerResponse"/> </operation> </portType> <binding name="HelloClassBinding" type="tns:HelloClassPortType"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/htt p"/> <operation name="HelloWho"> <soap:operation/> <input> <soap:body namespace="urn:HelloService" encodingStyle="http://s chemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body namespace="urn:HelloService" encodingStyle="http://sc hemas.xmlsoap.org/soap/encoding/"/> </output> </operation> </binding> <service name="HelloName"> <documentation>

This is the Hello Name Interface </documentation> <port name="HelloClass" binding="tns:HelloClassBinding"> <soap:address location="http://www.gw4y.com/helloworld/hello_server. php"/> </port> </service> </definitions> -------------------------------------------------------------------------------This is the minimum required, but to test it on our server we'll create a simple client first you can call this hello_client.php on your server Code: -------------------------------------------------------------------------------<?php $sClient = new SoapClient('hello.wsdl'); $params1 = "CodeCall"; echo($sClient->HelloWho($params1)."<br>"); ?> -------------------------------------------------------------------------------it creates a SoapClient using the hello.wsdl interface, we give save a string "CodeCall" in the varable $params1, and send that to our sClient (soapClient) function "HelloWho" and echo the responce. At this point you should get "Hello CodeCall" Now to tall over the network to our client computer, so fire up Visual Studio & start a new project (I'm using a Windows Forms Application NET 3.5) First we want to connect to our Web Services, so right click on References & select Add Service Reference

CLICK "ADVANCED" on the next popup Click "Add Web Reference" In the URL you will give it your server address (or use the one I've got the files will be their for about a year ) and click "Add Reference"

You'll see the Web reference added to your project

Next we need to add the Reference to our project, open the code for your new Form1, and add the following using Code: using HelloServer.com.gw4y.www;

using (yournamespace).the URL to your server, note it's backwards TLD . Domain Name . WWW add a button to your form & doubleclik it to open the code section for a new button. Code: private void button1_Click(object sender, EventArgs e) { } Now to consume our new class (remember we called it HelloName, so add the class to your code like so Code: private void button1_Click(object sender, EventArgs e) { HelloName UseWebServerClass = new HelloName(); } we can now access our Webserver Function via the UsewebServerClass (or whatever name you wish to call it) Our final lines will send the string "CodeCall" to our HelloWho function on the server, and then display the Returned string via a Messagebox Code: private void button1_Click(object sender, EventArgs e) { HelloName UseWebServerClass = new HelloName(); string ReturnedFromServer; ReturnedFromServer = UseWebServerClass.HelloWho("CodeCall"); MessageBox.Show(ReturnedFromServer); } Giving you a nice "Hello CodeCall" Popup on your desktop application that is now talking to a PHP Server on the internet

Você também pode gostar