...

View Full Version : php consumed webservice



mrcurtains
03-27-2009, 06:18 PM
Hi,
I am trying to connect to an asp.net webservice through php. I have been working on it for days and cannot seem to get it to work. My php knowledge is very limited.

The result that I am expecting is that the webservice will return the values of the 2 parameters passed to it. However, it only returns the first parameter and I can't see why. I would be very grateful for any help. Thank you


<?

//create the web service client
$objClient = new SoapClient("https://localhost/webservice/mywebservice.asmx?wsdl");

//Create the parameters to be sent to the webservice
$params->myFirstinput = 'Hello';
$params->mySecondInput = 'World';

//call the web service
$objWS = $objClient->HelloWorld($params);

//print results to the browser
foreach ($objWS as $result)
echo $result;

?>

The Code within the webservice is:


<WebMethod()> _
Public Function HelloWorld(ByVal myinput As String) As String
Dim returned As String = ""
For Each val As String In myinput
returned = returned & val
Next

Return returned
End Function

Fou-Lu
03-27-2009, 07:26 PM
Lets take a look here.
SoapClient. Check.
Service __call. Check (via HelloWorld).
$objWS. Check.

The problem here is that you don't actually return two parameters (or more accurately, two items in an array), you're returning a string:


Public Function HelloWorld(ByVal myinput As String) As String


The function will concatenate the provided array into a single string (without spaces). Since it only takes a string, it will only return the one value, and not the expected two values (which cannot be returned except as an array). I don't know what you're $params object is either, since you have no declaration for it. That brings us to an incompatibility issue as well, $params is a complex type while string is a primitive type. I don't have you're wsdl, but I'd assume you're looking to declare you're service as taking a String[] and building you're $params as a standard PHP array. That will let you take an array of strings, concatenate them together, and return a string.

Does that help?

mrcurtains
03-28-2009, 02:49 PM
Hello Fou-Lu,
thank you for your reply. I tried declaring the HelloWorld service as being passed an array but still returning a string yet I still couldn't get it to work.

To make it simpler, I have changed the array into 2 string parameters, so the consuming php code should pass two strings to the HelloWorld service function with the values of "Hello" and "World".

The string I expect to be returned to the php should be "Returned String = Hello World" which is a concatenation of the two parameters. However, all I am getting is the initial instatiation of 'returned' which is "Returned String = ". It appears that neither of the parameter strings are being passed to the webservice and I can't see why.

<?

//create the web service client
$objClient = new SoapClient("https://localhost/webservice/mywebservice.asmx?wsdl");

//call the web service
$objWS = $objClient->HelloWorld('Hello', 'World');

//print results to the browser
foreach ($objWS as $result)
echo $result;

?>

<WebMethod()> _
Public Function HelloWorld(ByVal myinput1 As String, ByVal myinput2 As String) As String
'create the string to be returned
Dim returned As String = "Returned String = "
'concatenate the two parameters to the end of the returned string
returned = returned & myinput1 & " " & myinput2

Return returned
End Function

The wsdl is as follows:


<?xml version="1.0" encoding="utf-8" ?>
- <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:tns="http://www.myWebService.com"
xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
targetNamespace="http://www.myWebService.com"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">eturns a string of Hello World</wsdl:documentation>
- <wsdl:types>
- <s:schema elementFormDefault="qualified" targetNamespace="http://www.myWebService.com">
- <s:element name="HelloWorld">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="myinput" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="mySecondInput" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="HelloWorldResponse">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
- <wsdl:message name="HelloWorldSoapIn">
<wsdl:part name="parameters" element="tns:HelloWorld" />
</wsdl:message>
- <wsdl:message name="HelloWorldSoapOut">
<wsdl:part name="parameters" element="tns:HelloWorldResponse" />
</wsdl:message>
- <wsdl:portType name="hwSoap">
- <wsdl:operation name="HelloWorld">
<wsdl:input message="tns:HelloWorldSoapIn" />
<wsdl:output message="tns:HelloWorldSoapOut" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="hwSoap" type="tns:hwSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="HelloWorld">
<soap:operation soapAction="http://www.myWebService.com/HelloWorld" style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:binding name="hwSoap12" type="tns:hwSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="HelloWorld">
<soap12:operation soapAction="www.myWebService.com/HelloWorld" style="document" />
- <wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="hw">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Returns Hello orld</wsdl:documentation>
- <wsdl:port name="hwSoap" binding="tns:hwSoap">
<soap:address location="http://www.myWebService.com/hw.asmx" />
</wsdl:port>
- <wsdl:port name="hwSoap12" binding="tns:hwSoap12">
<soap12:address location="http://www.myWebService.com/hw.asmx" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

Fou-Lu
03-28-2009, 07:12 PM
Definitely looks like the WSDL.
I'll see if I can fix this when I get home, but I'll warn you that I don't have the technology available for use - I'll be writing the wsdl and using PHP soapServer instead of .NET.

mrcurtains
03-31-2009, 05:31 PM
Ok great, thank you for your help. I have found a site, http://uk2.php.net/soap, that says it won't work because I am running the webservice from a windows server so need to use NuSOAP instead. I'll give that a try.

mrcurtains
04-08-2009, 10:20 AM
Right, after painfully banging my head against a wall for 2 weeks, I managed to find out what the error was.

When creating the client webservice in php I used the following code:

$objClient = new SoapClient("https://localhost/webservice/mywebservice.asmx?wsdl");
The client web service was using the wsdl to see what methods and parameters the web service has. The reason that I could only return one parameter was because the php client was caching the wsdl from when I set it up. Even though I changed the server web service the php client was not seeing the changes. I didn't spot this because when I looked at the wsdl directly, it showed the new changes. I used the following debugging code to see that the php client was not seeing the updated wsdl.

$objClient = new SoapClient('https://localhost/webservice/mywebservice.asmx?wsdl',array('trace'=>1));
// See the functions webservice is exposing
echo '<pre>';
print_r($objClient ->__getFunctions());
Once I reaslised that the error was due to caching I used the following code to stop the php client from looking at the old wsdl.

$objClient = new SoapClient('https://localhost/webservice/mywebservice.asmx?wsdl', array('cache_wsdl' => 0));
Such a simple issue but it stumped me for a while. I hope this helps anyone else with the same problems.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum