1
votes

i am new to SOAP . I am trying to integrate my application to SmartTurn webservice API(its a inventry management company). They are SOAP and wsdl for their API operations. i am new to soap so i have no idea that how i can do this in php. here is xml that i want to use.

<?xml version="1.0" encoding="UTF-8" ?><soapenv:Envelope      xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <soapenv:Body><saveSalesOrder 
xmlns="http://www.smartturn.com/services/OccamService/sales-order"> 
<inCredential> 
   <ns1:UserId 
xmlns:ns1="http://www.smartturn.com/services/occamtypes">[email protected]</ns1:UserId>  
  <ns2:Password 
xmlns:ns2="http://www.smartturn.com/services/occamtypes">secret</ns2:Password>  
</inCredential> 
<inSalesOrders> 
  <ns3:externalNumber xmlns:ns3="http://www.smartturn.com/services/sales-order-types">Ext-id-00502</ns3:externalNumber>  
  <ns4:type xmlns:ns4="http://www.smartturn.com/services/sales-order-types">EXTERNAL</ns4:type>  
  <ns5:date xmlns:ns5="http://www.smartturn.com/services/sales-order-types">2012-10-20T00:04:34.479Z</ns5:date>  
 <ns6:dateDue xmlns:ns6="http://www.smartturn.com/services/sales-order-types">2012-10-20T00:04:34.479Z</ns6:dateDue>  
  <ns7:customerName xmlns:ns7="http://www.smartturn.com/services/sales-order-types">customer name</ns7:customerName>  
  <ns8:customerContact 
xmlns:ns8="http://www.smartturn.com/services/sales-order-types">customer contact</ns8:customerContact>  
  <ns9:customerContactPhone 
xmlns:ns9="http://www.smartturn.com/services/sales-order-types">customer phone</ns9:customerContactPhone>  
  <ns10:customerAddress 
xmlns:ns10="http://www.smartturn.com/services/sales-order-types"> 
    <ns11:addressLine1 
xmlns:ns11="http://www.smartturn.com/services/occamtypes">the main street</ns11:addressLine1>  
    <ns12:addressLine2 
xmlns:ns12="http://www.smartturn.com/services/occamtypes">broadway</ns12:addressLine2>  
    <ns13:city 
xmlns:ns13="http://www.smartturn.com/services/occamtypes">Oakland</ns13:city>  
    <ns14:state 
xmlns:ns14="http://www.smartturn.com/services/occamtypes">California</ns14:state>  
    <ns15:country 
xmlns:ns15="http://www.smartturn.com/services/occamtypes">USA</ns15:country>  
    <ns16:postalCode 
xmlns:ns16="http://www.smartturn.com/services/occamtypes">94607</ns16:postalCode>  
  </ns10:customerAddress> 
  <ns17:salesRep xmlns:ns17="http://www.smartturn.com/services/sales-order-types">sales rep</ns17:salesRep>  
  <ns18:useShipToAsBillAddress 
xmlns:ns18="http://www.smartturn.com/services/sales-order-types">false</ns18:useShipToAsBillAddress>  
  <ns19:shipToName xmlns:ns19="http://www.smartturn.com/services/sales-order-types">ship to name</ns19:shipToName>  
  <ns20:shipToContact 
xmlns:ns20="http://www.smartturn.com/services/sales-order-types">ship to contact</ns20:shipToContact>  
  <ns21:shipToContactPhone 
xmlns:ns21="http://www.smartturn.com/services/sales-order-types">ship to contact phone</ns21:shipToContactPhone>  
  <ns22:status xmlns:ns22="http://www.smartturn.com/services/sales-order-types">NEW</ns22:status>  
  <ns23:item xmlns:ns23="http://www.smartturn.com/services/sales-order-types"> 
    <ns23:itemMasterId>BB-10</ns23:itemMasterId>  
    <ns23:description>this is a desc</ns23:description>  
    <ns23:details>these are details</ns23:details>  
    <ns23:orderedQuantity> 
<ns26:value 
xmlns:ns26="http://www.smartturn.com/services/occamtypes">10.0</ns26:value>  
<ns27:unitAbbreviation 
xmlns:ns27="http://www.smartturn.com/services/occamtypes">ea</ns27:unitAbbreviation>  
    </ns23:orderedQuantity> 
  <ns23:customerRequestDate>2007-03-
20T00:04:34.479Z</ns23:customerRequestDate>  
  <ns23:price> 
<ns28:value 
xmlns:ns28="http://www.smartturn.com/services/occamtypes">100.0</ns28:value>  
<ns29:type 
xmlns:ns29="http://www.smartturn.com/services/occamtypes">$10</ns29:type>  
  </ns23:price> 
  <ns23:vendorItemId>vendor item id</ns23:vendorItemId>  
  <ns23:manufacturerItemId>manf item id</ns23:manufacturerItemId>  
  <ns23:upcCode>UPC Code</ns23:upcCode>  
  <ns23:manufacturerId>manf id</ns23:manufacturerId>  
</ns23:item> 
<ns30:comments xmlns:ns30="http://www.smartturn.com/services/sales-order-types">This is a comment</ns30:comments>  
</inSalesOrders> 
</saveSalesOrder> 
</soapenv:Body> 
</soapenv:Envelope>

`

and the url is $url = "https://services.smartturn.com/occam/services/OccamService?wsdl"; and the operation that i want to perform is 'saveSalesOrder' how can i do that in php.

Thanks in advance .

2

2 Answers

2
votes

First, you'll need PHP's SOAP extension installed and enabled. Then:

$url = "https://services.smartturn.com/occam/services/OccamService?wsdl";

$data = array(
    "UserId"               => "secret",
    "Password"             => "secret",
    "type"                 => "EXTERNAL",
    "date"                 => "2012-10-20T00:04:34.479Z",
    "dateDue"              => "2012-10-20T00:04:34.479Z",
    "externalNumber"       => "Ext-id-00502",
    "customerName"         => "test ",
    "customerContact"      => "customer Contact",
    "customerContactPhone" => "123456789",
    "salesRep"             => "w salesRep",
);

try {
    $soapClient = new SoapClient($url, array(
        'trace' => true, // for debugging, disable in production
        //"connection_timeout" => 1
    ));

    // List types to get an idea how to call methods, since it looks like
    // there are no help pages as in .asmx services
    // var_dump($soapClient->__getTypes());

    $soapClient->saveSalesOrder($data);
} catch (Exception $e) {
    trigger_error("SOAP error: ".$e->getMessage(), E_USER_WARNING);
}

Links:

Overall SmartTurn service listing

PHP SoapClient documentation

Edit: updated example based on comment.

0
votes

This is a great answer from Halil and really helped me to figure out what to do. You will need to add more arrays to create your request as the nested values of the XML request need to be put in arrays. Also there is a minimum number of fields to fill out, so you're best to look at the API doc and see the sample request, then format your arrays to match the nested values. For example:

$url = "https://stdemo.smartturn.com/occam/services/OccamService?wsdl"; //DEMO URL
// $url = "https://app.smartturn.com/occam/services/OccamService?wsdl"; //LIVE URL

$data = array(
"inCredential" => array("UserId" => "secret", "Password" => "secret"),
"inSalesOrders" => array("type" => "EXTERNAL",
                         "date" => "2012-10-20T00:04:34.479Z",
                         "dateDue" => "2012-10-20T00:04:34.479Z",
                         "externalNumber" => "Ext-id-00502",
                         "customerName" => "test ",
                         "customerContact" => "customer Contact",
                         "customerContactPhone" => "123456789",
                         "salesRep" => "w salesRep",
                          ...
                    )
);

try {
$soapClient = new SoapClient($url, array(
    'trace' => true, // for debugging, disable in production
    //"connection_timeout" => 1
));

// List types to get an idea how to call methods, since it looks like
// there are no help pages as in .asmx services
// var_dump($soapClient->__getTypes());

$soapClient->saveSalesOrder($data);
} catch (Exception $e) {
    trigger_error("SOAP error: ".$e->getMessage(), E_USER_WARNING);
}

The only issue with using this method for generating the SOAP envelopes is that as you are using an associative array, you can only sent 1 item object with each sales order request. As soon as you have multiple items in an order, this doesn't work. You could investigate SoapVar, I ended up forming the complete XML document and then sending it with CURL.