Constructing Soap Request in PHP
New to soap, I've used soapUI (http://www.soapui.org) to test the service I need to access and determined that I need the soap request to look like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_5=mynamespace>
<soapenv:Header/>
<soapenv:Body>
<_5:GetStaff>
<_5:Request>
<_5:SourceCredentials>
<_5:SourceName>name</_5:SourceName>
<_5:Password>pword</_5:Password>
<_5:SiteIDs>
<_5:int>num</_5:int>
</_5:SiteIDs>
</_5:SourceCredentials>
</_5:Request>
</_5:GetStaff>
</soapenv:Body>
</soapenv:Envelope>
where mynamespace, name, pword, and num are all placeholders. I just need to construct this request in PHP. This is my latest attempt:
<html>
<body>
<?php
require_once('nusoap.php');
$wsdl = ...;
$client = new SoapClient($wsdl);
/*
echo("\nDumping client object:\n");
var_dump($client);
echo("\nDumping client object functions:\n");
var_dump($client->__getFunctions());
*/
try {
$credentials = new SoapVar(array(
'SourceName'=>name,
'Password'=>pword,
'SiteIDs'=>num
),
SOAP_ENC_OBJECT,
type1,
mynamespace
);
$request = new SoapVar(array('SourceCredentials'=>$credentials),
SOAP_ENC_OBJECT,
type2,
mynamespace
);
$result = $client->GetStaff(new SoapParam($request, "Request"));
echo $result;
}
catch (SoapFault $fault) {
trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
}
print "<pre>\n";
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
print "</pre>";
?>
</body>
</html>
Take my word for it that type1 is the appropriate type for the SourceCredentials tag and that type2 is the appropriate type for the Request tag.
When I open this file in a browser*, absolutely nothing shows up. When the commented lines are uncommented, I can see the dump of the object (although I have no idea how to go about interpreting the dump), but the object functions are NOT displayed.
Does this sound like the SoapClient
itself isn't set up properly, or is the error in the construction of the parameters for the soap call?
*Clarification: I've named this file "index.php
," and in a browser I'm navigating to the directory in which this file is located (it is a sub-directory of my public_html
directory).
Dumping client object: object(soapclient)#1 (48) { ["username"]=> string(0) "" ["password"]=> string(0) "" ["authtype"]=> string(0) "" ["certRequest"]=> array(0) { } ["requestHeaders"]=> bool(false) ["responseHeaders"]=> string(0) "" ["responseHeader"]=> NULL ["document"]=> string(0) "" ["endpoint"]=> string(64) "foobar.axmx?wsdl" ["forceEndpoint"]=> string(0) "" ["proxyhost"]=> bool(false) ["proxyport"]=> bool(false) ["proxyusername"]=> bool(false) ["proxypassword"]=> bool(false) ["portName"]=> string(0) "" ["xml_encoding"]=> string(0) "" ["http_encoding"]=> bool(false) ["timeout"]=> int(0) ["response_timeout"]=> int(30) ["endpointType"]=> string(4) "soap" ["persistentConnection"]=> bool(false) ["defaultRpcParams"]=> bool(false) ["request"]=> string(0) "" ["response"]=> string(0) "" ["responseData"]=> string(0) "" ["cookies"]=> array(0) { } ["decode_utf8"]=> bool(true) ["operations"]=> array(0) { } ["curl_options"]=> array(0) { } ["bindingType"]=> string(0) "" ["use_curl"]=> bool(false) ["fault"]=> NULL ["faultcode"]=> NULL ["faultstring"]=> NULL ["faultdetail"]=> NULL ["title"]=> string(6) "NuSOAP" ["version"]=> string(5) "0.9.5" ["revision"]=> string(18) "$Revision: 1.123 $" ["error_str"]=> string(0) "" ["debug_str"]=> string(305) "2011-05-12 17:10:31.480099 soapclient: ctor wsdl= timeout=0 response_timeout=30 endpoint=string(64) "foobar.asmx?wsdl" 2011-05-12 17:10:31.480593 soapclient: instantiate SOAP with endpoint at foobar.asmx?wsdl " ["charencoding"]=> bool(true) ["debugLevel"]=> int(9) ["XMLSchemaVersion"]=> string(32) "http://www.w3.org/2001/XMLSchema" ["soap_defencoding"]=> string(10) "ISO-8859-1" ["namespaces"]=> array(4) { ["SOAP-ENV"]=> string(41) "http://schemas.xmlsoap.org/soap/envelope/" ["xsd"]=> string(32) "http://www.w3.org/2001/XMLSchema" ["xsi"]=> string(41) "http://www.w3.org/2001/XMLSchema-instance" ["SOAP-ENC"]=> string(41) "http://schemas.xmlsoap.org/soap/encoding/" } ["usedNamespaces"]=> array(0) { } ["typemap"]=> array(6) { ["http://www.w3.org/2001/XMLSchema"]=> array(43) { ["string"]=> string(6) "string" ["boolean"]=> string(7) "boolean" ["float"]=> string(6) "double" ["double"]=> string(6) "double" ["decimal"]=> string(6) "double" ["duration"]=> string(0) "" ["dateTime"]=> string(6) "string" ["time"]=> string(6) "string" ["date"]=> string(6) "string" ["gYearMonth"]=> string(0) "" ["gYear"]=> string(0) "" ["gMonthDay"]=> string(0) "" ["gDay"]=> string(0) "" ["gMonth"]=> string(0) "" ["hexBinary"]=> string(6) "string" ["base64Binary"]=> string(6) "string" ["anyType"]=> string(6) "string" ["anySimpleType"]=> string(6) "string" ["normalizedString"]=> string(6) "string" ["token"]=> string(6) "string" ["language"]=> string(0) "" ["NMTOKEN"]=> string(0) "" ["NMTOKENS"]=> string(0) "" ["Name"]=> string(0) "" ["NCName"]=> string(0) "" ["ID"]=> string(0) "" ["IDREF"]=> string(0) "" ["IDREFS"]=> string(0) "" ["ENTITY"]=> string(0) "" ["ENTITIES"]=> string(0) "" ["integer"]=> string(7) "integer" ["nonPositiveInteger"]=> string(7) "integer" ["negativeInteger"]=> string(7) "integer" ["long"]=> string(7) "integer" ["int"]=> string(7) "integer" ["short"]=> string(7) "integer" ["byte"]=> string(7) "integer" ["nonNegativeInteger"]=> string(7) "integer" ["unsignedLong"]=> string(0) "" ["unsignedInt"]=> string(0) "" ["unsignedShort"]=> string(0) "" ["unsignedByte"]=> string(0) "" ["positiveInteger"]=> string(0) "" } ["http://www.w3.org/2000/10/XMLSchema"]=> array(11) { ["i4"]=> string(0) "" ["int"]=> string(7) "integer" ["boolean"]=> string(7) "boolean" ["string"]=> string(6) "string" ["double"]=> string(6) "double" ["float"]=> string(6) "double" ["dateTime"]=> string(6) "string" ["timeInstant"]=> string(6) "string" ["base64Binary"]=> string(6) "string" ["base64"]=> string(6) "string" ["ur-type"]=> string(5) "array" } ["http://www.w3.org/1999/XMLSchema"]=> array(11) { ["i4"]=> string(0) "" ["int"]=> string(7) "integer" ["boolean"]=> string(7) "boolean" ["string"]=> string(6) "string" ["double"]=> string(6) "double" ["float"]=> string(6) "double" ["dateTime"]=> string(6) "string" ["timeInstant"]=> string(6) "string" ["base64Binary"]=> string(6) "string" ["base64"]=> string(6) "string" ["ur-type"]=> string(5) "array" } ["http://soapinterop.org/xsd"]=> array(1) { ["SOAPStruct"]=> string(6) "struct" } ["http://schemas.xmlsoap.org/soap/encoding/"]=> array(3) { ["base64"]=> string(6) "string" ["array"]=> st开发者_如何转开发ring(5) "array" ["Array"]=> string(5) "array" } ["http://xml.apache.org/xml-soap"]=> array(1) { [0]=> string(3) "Map" } } ["xmlEntities"]=> array(5) { ["quot"]=> string(1) """ ["amp"]=> string(1) "&" ["lt"]=> string(1) "<" ["gt"]=> string(1) ">" ["apos"]=> string(1) "'" } }
This ended up being a configuration issue; the configuration of php on the servers I was using did not have soap enabled. Additionally, I did not have admin permissions on the servers, so it was difficult to determine this as the cause and then impossible to fix.
I had to set up my own machine as a server, and I am now able to successfully set up a SoapClient.
If anyone else goes through something similar and needs to do the same thing, I found a good tutorial on how to set up an Apache server on a Windows machine with PHP and MySQL here: http://www.overclockers.com/forums/showthread.php?t=272080
Construct your SoapClient like this:
$client = new SoapClient($wsdl, array('trace' => 1,
'exceptions' => true,
'cache_wsdl' => WSDL_CACHE_NONE));
You need the trace option to be able to see the output of __getLastRequest()
精彩评论