1

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.

2
  • Can you run this on your wsdl and post the part about ProcessFreightRate here? $client = new SoapClient('yourwsdl'); var_dump($client->__getTypes()); Commented Sep 8, 2011 at 17:59
  • Would this just return the XML Schema? I could post a portion of that if you would like, as the markup is a ton cleaner than what var_dump() spit out. Commented Sep 8, 2011 at 18:12

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Great. Thank you. Also, in examples I've seen for XML documents sent using SOAP, the names of the XML elements were prepended with "soap:" and there was a Header Section outlining some information about the document. Are these addition necessary as well? This is my first experience using SOAP and I'm quite lost!
Don't know, actually. I've never used soap either

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.