Reputation: 1215
I am attempting to create a UPS Freight Rating Web Service using PHP and SOAP. UPS provides a WSDL for the SOAP Client (the important part of which I've pasted below).
My question is, how do I construct the XML document to send using the SOAP Client? I have seen conflicting reports on whether or not to format my request as a PHP Array or a giant string. Once this document is constructed, how should the request be made through the SOAP Client I've created in PHP?
Portion of the WSDL:
<wsdl:binding name="FreightRateBinding" type="tns:FreightRatePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="ProcessFreightRate">
<soap:operation soapAction="http://onlinetools.ups.com/webservices/FreightRateBinding/v1.0" style="document"/>
<wsdl:input name="RateRequest">
<soap:body parts="Body" use="literal"/>
<soap:header message="tns:RateRequestMessage" part="UPSSecurity" use="literal">
<soap:headerfault message="tns:RateErrorMessage" part="RateError" use="literal"/>
</soap:header>
</wsdl:input>
<wsdl:output name="RateResponse">
<soap:body parts="Body" use="literal"/>
</wsdl:output>
<wsdl:fault name="RateError">
<soap:fault name="RateError" use="literal"/>
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
Portion of my XML Document, so far:
<?xml version="1.0" ?>
<FreightRateRequest>
<Request>
<RequestOption>1</RequestOption>
</Request>
<ShipFrom>
<Address>
<Name>Test</Name>
<AddressLine1>17 MacDade Blvd</AddressLine1>
<City>Collingdale</City>
<PostalCode>19023</PostalCode>
<CountryCode>US</CountryCode>
<Phone>
<Number>1-800-249-0011</Number>
</Phone>
</Address>
</ShipFrom>
<ShipperNumber>21W17V</ShipperNumber>
<ShipTo>
<Name>Test</Name>
<AddressLine1>14908 Sandy Lane</AddressLine1>
<City>San Jose</City>
<PostalCode>95124</PostalCode>
<CountryCode>US</CountryCode>
</ShipTo>
PHP, so far:
$mySOAP = new SoapClient("FreightRate.wsdl", $myOptionsArray);
Any help would be greatly appreciated. I will also reply with any additional information, if needed.
Upvotes: 1
Views: 3440
Reputation: 360562
XML is a serious pain to generate using DOM operations. You'll save yourself a ton of hair pulling by just treating it as a giant string and inserting the relevant values directly:
$xml = <<<EOL
<?xml blah blah blah
<root>
<tag>$some_value</tag>
<othertag>$different_value</othertag>
</root>
EOL;
As long as you take precautions to ensure that the contents of the variables make for valid XML, this is by far the easiest method. That means convert any HTML character entities that aren't the 5 XML accepts (<>"'&
) into their raw character format, and escape those 5 characters into entity equivalent.
Upvotes: 2