views:

65

answers:

2

I have a Hello World c# ASP .NET web service which is automatically generated by visual studio web service project. I want to call it from a php client. Can I know how to do it? Better if anyone can provide with small code example.. I dont have much experience in php and no in depth understanding of web services so finding difficult to do this..

Thanks

+1  A: 

Assuming it's a SOAP web service, you should just be able to use the normal php SOAP library. Now SOAP portability isn't quite everything it's cracked up to be in my experience, so you may need to fiddle a bit (in particular, test things like how empty arrays work) but that should be a good starting point.

The docs I've linked to look pretty good, but if you search for "php SOAP tutorial" you get lots of hits which will take you through step-by-step.

Another option is nusoap. I can't comment on which implementation is better.

Jon Skeet
Thanks. I thought of using nusoap
chathuradd
+1  A: 

Since PHP is a dynamic language, its pretty straight forward. All you need is a library (SoapUI) and the WSDL. If you have the location (URL) of the web service add a ?WSDL to the end of the URL and you have the definition. Then its just calling the service from there.

<?php
require_once('libs/nusoap.php');
$wsdl="http://thedomain.com/theservice/endpoing.svc?wsdl";
$client=new soapclient($wsdl, 'wsdl');
$param=array('number1'=>'2', 'number2'=>'3');
echo $client->call('add', $param);
?>

You can find the library here: http://www.soapui.org/

Benny
thanks. I have the url of the web service and I installed soapui as well. Can I know what did you mean by require_once('libs/nusoap.php') ?It can't find the path 'libs/nusoap.php' and gives the following error.Failed opening required 'libs/nusoap.php' (include_path='.;C:\php5\pear') in C:\wamp\www\hello.php on line 6
chathuradd
I got it. I m using nusoap as the soap client library. I tried to call the HelloWorld method of the web service using following: require_once('lib/nusoap.php'); $wsdl="http://localhost:64226/Service1.asmx?wsdl"; $client=new soapclient($wsdl, 'wsdl'); echo $client->call('HelloWorld');But it does not return the output "Hello World". Instead prints the word "Array". Any idea ? I know this must be a very basic question i m asking :)
chathuradd
Sorry, I'm used to PHP4, so an even better solution for you if you have PHP5 is to use the built in SoapClient. Example code (tested) runs like so:[PHP Client]<?phpclass SayHello { public $name = 'Benny';}$client = new SoapClient('http://localhost/WebService1/Service1.asmx?wsdl');$result = $client->SayHello(new SayHello());echo $result->SayHelloResult;?>[ASMX Service][WebMethod]public string SayHello(String name){ return "Hello " + name;}
Benny
thanks a lot Benny! It worked for me using SoapClient in php 5. Initially I got an error saying it is undefined and then I realized that I had not enabled php_soap extension in WAMP. After enabling it worked :)
chathuradd