Consuming a soap webservice

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rrhandle
Forum Newbie
Posts: 10
Joined: Thu Jan 01, 2004 1:51 pm
Location: Wisconsin

Consuming a soap webservice

Post by rrhandle »

I am new to PHP. I have a php page that calls a webservice using soap, and returns a soap response, but I have no idea how to work with this.
The response is listed below. Any help greatly appreciated.

[Admin Edit: added line breaks to allow code to wrap, added code tags for clarity]

Code: Select all

HTTP/1.1 200 OK 
Server: Microsoft-IIS/5.0 
Date: Tue, 13 Jan 2004 07:46:32 GMT 
X-Powered-By: ASP.NET 
X-AspNet-Version: 1.1.4322 
Cache-Control: private, max-age=0 
Content-Type: text/xml; charset=utf-8 
Content-Length: 3528 
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Get_ProfileResponse xmlns="ns.www.astromatchmaker.com">
<Get_ProfileResult>
<NewDataSet xmlns="">
<temp>
<Member_ID>1</Member_ID>
<Username>guest</Username>
<Password>123456</Password>
<City>Appleton</City>
<State_Provance>WI</State_Provance>
<Country>USA</Country>
<Email>guest@astro-matchmaker.com</Email>
<Birth_Date>19770426</Birth_Date>
<Birth_Time>11:50</Birth_Time>
<Birth_AMPM>AM</Birth_AMPM>
<Birth_City>Appleton</Birth_City>
<Birth_State_Provance>WI</Birth_State_Provance>
<Birth_Country>USA</Birth_Country>
<Seeking>MW</Seeking>
<Target_Age_Low>18</Target_Age_Low>
<Target_Age_High>99</Target_Age_High>
<Subscription_Code>NA</Subscription_Code>
<Zip_Code>54956</Zip_Code>
</temp>
<temp>
<Username>GUEST5</Username>
<Password>123456</Password>
<City>APPLETON</City>
<State_Provance>AL</State_Provance>
<Country>AFG</Country>
<Email>ASDF5@ASDF.COM</Email>
<Birth_Date>19020101</Birth_Date>
<Birth_Time>01:01</Birth_Time>
<Birth_AMPM>PM</Birth_AMPM>
<Birth_City>APPLETON</Birth_City>
<Birth_State_Provance>AL</Birth_State_Provance>
<Birth_Country>AFG</Birth_Country>
<Seeking>MW</Seeking>
<Target_Age_Low>28</Target_Age_Low>
<Target_Age_High>18</Target_Age_High>
<Subscription_Code>NA</Subscription_Code>
</temp>
<temp>
<Username>GUEST6</Username>
<Password>123456</Password>
<City>APPLETON</City>
<State_Provance>AL</State_Provance>
<Country>AFG</Country>
<Email>ASDF6@ASDF.COM</Email>
<Birth_Date>19020101</Birth_Date>
<Birth_Time>01:01</Birth_Time>
<Birth_AMPM>PM</Birth_AMPM>
<Birth_City>APPLETON</Birth_City>
<Birth_State_Provance>AL</Birth_State_Provance>
<Birth_Country>AFG</Birth_Country>
<Seeking>MW</Seeking>
<Target_Age_Low>28</Target_Age_Low>
<Target_Age_High>18</Target_Age_High>
<Subscription_Code>NA</Subscription_Code>
</temp>
<!-- snipped -->
</NewDataSet>
</Get_ProfileResult>
</Get_ProfileResponse>
</soap:Body>
</soap:Envelope>
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

1. research a bit about webservices

then go http://dietrich.ganx4.com/nusoap/index.php you can use NuSoap to consume a webservice
rrhandle
Forum Newbie
Posts: 10
Joined: Thu Jan 01, 2004 1:51 pm
Location: Wisconsin

Post by rrhandle »

I am using nusoap. Can you tell me how to use it in order to extract the data?
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

i dont know what does that web service do.. but you can look at this tutorial .. http://www.zend.com/zend/tut/tutorial-campbell.php
rrhandle
Forum Newbie
Posts: 10
Joined: Thu Jan 01, 2004 1:51 pm
Location: Wisconsin

Post by rrhandle »

Ya, darn, I've been there a trillion times in the last few days.

Here is the webservice. Just click the button. Look at the response, and tell me how I can get that info in an array or some other format so I can work with it.

http://www.astro-matchmaker.com/php_sam ... rofile.php
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

my question is... do you try to do it.. do you have some code?.
rrhandle
Forum Newbie
Posts: 10
Joined: Thu Jan 01, 2004 1:51 pm
Location: Wisconsin

Post by rrhandle »

In the microsoft world, we slap it into an ActiveX Dom object. You can then work with it as an array. I was hoping PHP had some object or function that would let you do that. Have try dozens of different code snippet, but I cannot get any further than print that one cryptic paragraph at the very top of the page using the Print_r.

Here is the complete code for that page:
[Admin Edit: added PHP tags for clarity.]

Code: Select all

<?
$strDB = "members_demo";		// [members | members_demo]
$strTokenID = "xxx111111x";		// xxx111111x forces connection to demo database.
$strErrors = "";
$strWhere = "";
$xml_file = 'f:\home\nomax\astro-matchmaker.com\www\databases\az.xml';

if(isset($_POST['SubmitGetProfile'])) {
	$strWhere = $_POST['strWhere'];
	//echo $strWhere;
	$SoapMsg = "";
	$SoapMsg = $SoapMsg . "<Get_Profile xmlns='ns.www.astromatchmaker.com'>";
	$SoapMsg = $SoapMsg . "<strTokenID>" . $strTokenID . "</strTokenID>";
	$SoapMsg = $SoapMsg . "<strDB>" . $strDB . "</strDB>";
	$SoapMsg = $SoapMsg . "<strWhere>" . $strWhere . "</strWhere>";
	$SoapMsg = $SoapMsg . "</Get_Profile>";

	include("NuSOAP/nusoap-0.6.1/nusoap.php");
	$soapclient = new soapclient('http://www.astro-matchmaker.com/aspnet_samples/MatchMaker.asmx?wsdl',true);
	$params = array($SoapMsg);
	$result = $soapclient -> call("Get_Profile", $params);

echo $SoapMsg;
echo $result;


print_r($result);


//Display the request and response
echo '<h2>Request</h2>';
echo '' . htmlspecialchars($soapclient->request, ENT_QUOTES) . '';
echo '<h2>Response</h2>';
echo '' . htmlspecialchars($soapclient->response, ENT_QUOTES) . '';

	// kill object
	unset($soapclient);
}


?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">

<table width="300" border="0" align="center">
<tr>
<td nowrap>
<div align="center">
<font color="red">
	<? print $strErrors; ?>
</font>
</div>
</td>
</tr>
</table>
<form method="post" action="Get_Profile.php">
    <table align='center' bgcolor='#000000' cellpadding="0" cellspacing="0" border="1">
<tr>
<td>
    <table width="100%" border="0" bgcolor="#FFFFFF">
    <tr>
    <td width="28%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td width="17%" nowrap>

    <div align="right"><font face="Verdana, Arial, Helvetica, sans-serif"><font face="Verdana, Arial, Helvetica, sans-serif"><font size="3"><font size="2"></font></font></font></font></div>
	</td>
	<td nowrap width="37%">
	<h1><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Get Profile(s)</font></h1>
	</td>
	<td nowrap width="18%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	</tr>

    <tr>

    <td width="28%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td bgcolor="efefef" width="17%" nowrap>

    <div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Where:&nbsp;</font></div>
	</td>
	<td nowrap width="37%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
	<input type="text" name="strWhere" size="60" value="username like 'guest%'">
	&nbsp;( Enter any valid WHERE clause )</font></td>
	<td nowrap width="18%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	</tr>
    <tr>
    <td width="28%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td width="17%" nowrap><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td nowrap width="37%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Examples:</b></font></td>
	<td nowrap width="18%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	</tr>
    <tr>
    <td width="28%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td width="17%" nowrap><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td nowrap width="37%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Where username like '%'  (returns all members)</font></td>
	<td nowrap width="18%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	</tr>
	<tr>
	<td width="28%">&nbsp;</td>
	<td width="17%" nowrap><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td nowrap width="37%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Where country = 'CAN' (returns all members from Canada)</font></td>
	<td nowrap width="18%">&nbsp;</td>
	</tr>
	<tr>
	<td width="28%">&nbsp;</td>
	<td width="17%" nowrap><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td nowrap width="37%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td nowrap width="18%">&nbsp;</td>
	</tr>
	<tr>
	<td width="28%">&nbsp;</td>
	<td width="17%" nowrap><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td nowrap width="37%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td nowrap width="18%">&nbsp;</td>
	</tr>
	<tr>
	<td width="28%">&nbsp;</td>
	<td width="17%" nowrap><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td nowrap width="37%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td nowrap width="18%">&nbsp;</td>
	</tr>
    <tr>
    <td width="28%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td width="17%" nowrap>
    <div align="right"><font face="Verdana, Arial, Helvetica, sans-serif"><font face="Verdana, Arial, Helvetica, sans-serif"><font size="3"><font size="2"></font></font></font></font></div>
	</td>
	<td nowrap width="37%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
	<input type="submit" name="SubmitGetProfile" value="submit">
		</font></td>
	<td nowrap width="18%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	</tr>

    <tr>

    <td width="28%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td width="17%" nowrap>

    <div align="right"><font face="Verdana, Arial, Helvetica, sans-serif"><font face="Verdana, Arial, Helvetica, sans-serif"><font size="3"><font size="2"></font></font></font></font></div>
	</td>
	<td nowrap width="37%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td nowrap width="18%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	</tr>

    <tr>

    <td width="28%">&nbsp;</td>
	<td width="17%" nowrap>

    <div align="right"><font face="Verdana, Arial, Helvetica, sans-serif"><font face="Verdana, Arial, Helvetica, sans-serif"><font size="3"><font size="2"></font></font></font></font></div>
	</td>
	<td nowrap width="37%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"></font></td>
	<td nowrap width="18%">&nbsp;</td>
	</tr>

    </table>
</td>
</tr>
</table>

</form>
</body>
</html>
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

well, nusoap handles it the same way, you can use the result as an array.

Just use the correct API.

after

Code: Select all

$result = $soapclient -> call("Get_Profile", $params);
try:

Code: Select all

foreach ($result['resultElements'] as $r) { 
   print_r($r);
}
rrhandle
Forum Newbie
Posts: 10
Joined: Thu Jan 01, 2004 1:51 pm
Location: Wisconsin

Post by rrhandle »

Tried it. Get the following error message:
Warning: Invalid argument supplied for foreach()
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

try

Code: Select all

<?php

         if ($result['faultstring']) { 
            echo '<h2>Error</h2>';
            echo $result['faultstring'];  //<-- show error if exists
         } else { 
                if (is_array($result['resultElements'])) {
                  foreach ($result['resultElements'] as $r) { 
                    print_r($r);
                   }
         }
?>
show the error
Last edited by AVATAr on Tue Jan 13, 2004 11:51 am, edited 1 time in total.
rrhandle
Forum Newbie
Posts: 10
Joined: Thu Jan 01, 2004 1:51 pm
Location: Wisconsin

Post by rrhandle »

??
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

echo $result['faultstring']; //<-- show error if exists
rrhandle
Forum Newbie
Posts: 10
Joined: Thu Jan 01, 2004 1:51 pm
Location: Wisconsin

Post by rrhandle »

It does not show an error using faultstring.

The error that appears on the page is referring to the first line of the foreach loop.

Code: Select all

foreach ($result['resultElements'] as $r) {
If I remove ['resultElements'], there is no error.
I tried to replace 'resultElements' with the name of some elements I was returning, but still get the same error: "Warning: Invalid argument supplied for foreach() in"

Was I right in trying to replace 'resultElements'?
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

nop, $result['resultElements'] is suposed to be an array of arrays.

i think the problem here is that, there's some kind of error in there.. so the $result['resultElements'] is not an array.

$result['faultstring'] is the error itself..

"Warning: Invalid argument supplied for foreach() in" is because the argument in the foreach is not an array.
rrhandle
Forum Newbie
Posts: 10
Joined: Thu Jan 01, 2004 1:51 pm
Location: Wisconsin

Post by rrhandle »

I don't know if this will help, but the following code returns all the data for the last member, yet as you can see on the screen, the soap response contains information for all members.

Code: Select all

foreach ($result as $r) {
 foreach ($r as $x) {
 	foreach ($x as $m) {
 	print_r($m);
 	print "<BR>";
 	}
 }
}
Post Reply