Convert C# to PHP -


i accessing webservice using soap , php. can connect number of functions through webservice api. 1 eludes me. have example in c#.

i have tried emulate example in php no luck.

i have attached c# code , attempt in php. error message included.

c# code

 public void makesale()  {      string yourkey = "your key";      using(dddsaleservice.saleserviceclient client = new saleserviceclient())      {          sale sale = client.startsale();          //header info          sale.clientnumber = 996001;          sale.date = datetime.now;          sale.employee = 1;          sale.noteid = 123;          sale.terminal = 1;          sale.type = saletype.sale;           //items in basket.          itemline line1 = new itemline();          line1.type = itemlinetype.sale;          line1.itemgroup = 1;          line1.supplier = 1;          line1.qty = 3;  //should -3 if return of goods.          line1.lineamount = 600;  //the normal amount of goods.          line1.edbnumber = 1; //unique key in our system. have if got articles our service.          line1.discountamount = -100;  //the discount.           //line2 comes here....           //add lines sale          sale.itemlines = new itemline[] { line1 };           //the payment lines          //1 cash, , 1 change. sale must sum 0 (itemlines + paymentlines)          paymentline cash = new paymentline();          cash.type = paymentlinetype.cash;          cash.qty = 1;          cash.lineamount = 600;           paymentline change = new paymentline();          change.type = paymentlinetype.change;          change.qty = 1;          change.lineamount = -100;           //add paymentlines          sale.paymentlines = new paymentline[]{cash,change};           //submit sale.          status response = client.savesale(sale, yourkey);          console.writeline(string.format("got response save sale saved: {0}, message: {1}",response.saved,response.message));          console.readline();     } } 

php code

$client = new soapclient("http://xxx.xxxxx.xxxx/xxxxxxx.svc?wsdl", array("trace" => 1, "connection_timeout" => 500));  // initialize sale // $client valid soap connection // has been setup earlier $sale = $client->startsale();  // output initalize sale returns print_r($sale);  // format order date/time $timezone = new datetimezone("europe/copenhagen"); $date = new datetime("now", $timezone); $order_date_time = $date->format("y-m-d\th:i:s");  // set header information $sale->startsaleresult->clientnumber = 996001; $sale->startsaleresult->date = $order_date_time; $sale->startsaleresult->employee = 1; $sale->startsaleresult->noteid = 123; $sale->startsaleresult->terminal = 1; $sale->startsaleresult->type = 'sale';  // itemline $line = new stdclass(); $line->type = 'sale'; $line->itemgroup = 1; $line->supplier = 1; $line->qty = 3; $line->lineamount = 600; $line->edbnumber = 1; $line->discountamount = 1-100; $sale->startsaleresult->itemlines->itemline[] = $line;  // payment line, cash $cash = new stdclass(); $cash->type = 'cash'; $cash->qty = 1; $cash->lineamount = 600; $sale->startsaleresult->paymentlines->paymentline[] = $cash;  // payment line, change $change = new stdclass(); $change->type = 'change'; $change->qty = 1; $change->lineamount = -100; $sale->startsaleresult->paymentlines->paymentline[] = $change;  // save sale $response = $client->savesale($sale->startsaleresult, 'xxxxxxxx');  print_r($response); 

output webservice returns when connecting

print_r($sale);  stdclass object (     [clientnumber] => 0     [date] => 0001-01-01t00:00:00     [employee] => 0     [itemlines] => stdclass object         (         )      [noteid] => 0     [paymentlines] => stdclass object         (         )      [terminal] => 0     [type] => sale ) 

error message

    [previous:exception:private] =>      [faultstring] => end element 'body' namespace 'http://schemas.xmlsoap.org/soap/envelope/' expected. found element 'param1' namespace ''. line 2, position 149.     [faultcode] => a:internalservicefault     [detail] => stdclass object         (             [exceptiondetail] => stdclass object                 (                     [helplink] =>                      [innerexception] =>                      [message] => end element 'body' namespace 'http://schemas.xmlsoap.org/soap/envelope/' expected. found element 'param1' namespace ''. line 2, position 149.                     [stacktrace] =>    @ system.xml.xmlexceptionhelper.throwxmlexception(xmldictionaryreader reader, string res, string arg1, string arg2, string arg3)    @ system.xml.xmlexceptionhelper.throwendelementexpected(xmldictionaryreader reader, string localname, string ns)    @ system.xml.xmlbasereader.readendelement()    @ system.servicemodel.channels.message.readfrombodycontentstoend(xmldictionaryreader reader, envelopeversion envelopeversion)    @ system.servicemodel.channels.message.readfrombodycontentstoend(xmldictionaryreader reader)    @ system.servicemodel.dispatcher.operationformatter.deserializebodycontents(message message, object[] parameters, boolean isrequest)    @ system.servicemodel.dispatcher.operationformatter.deserializerequest(message message, object[] parameters)    @ system.servicemodel.dispatcher.dispatchoperationruntime.deserializeinputs(messagerpc& rpc)    @ system.servicemodel.dispatcher.dispatchoperationruntime.invokebegin(messagerpc& rpc)    @ system.servicemodel.dispatcher.immutabledispatchruntime.processmessage5(messagerpc& rpc)    @ system.servicemodel.dispatcher.immutabledispatchruntime.processmessage4(messagerpc& rpc)    @ system.servicemodel.dispatcher.immutabledispatchruntime.processmessage3(messagerpc& rpc)    @ system.servicemodel.dispatcher.immutabledispatchruntime.processmessage2(messagerpc& rpc)    @ system.servicemodel.dispatcher.immutabledispatchruntime.processmessage1(messagerpc& rpc)    @ system.servicemodel.dispatcher.messagerpc.process(boolean isoperationcontextset)                     [type] => system.xml.xmlexception                 )          ) 

your arrays don't right. itemlines , paymentlines should arrays instead of object containing array.

even though startsale returns them objects, should still array call savesale. reason returned objects quirk of php soapclient. happens is, if there 1 element in array, soapclient gives object properties set single element properties. if there more 1 element in array, array of objects instead, , 1 level deeper result when there's single element. because returned no elements startsale, given objects instead of empty array.

... ... $sale->startsaleresult->itemlines = array($line);  // payment line, cash $cash = new stdclass(); $cash->type = 'cash'; $cash->qty = 1; $cash->lineamount = 600;  // payment line, change $change = new stdclass(); $change->type = 'change'; $change->qty = 1; $change->lineamount = -100; $sale->startsaleresult->paymentlines = array($cash, $change); 

also depending on wsdl looks like, may need pass associative array parameters, keys sale , key (according wsdl), instead of passing 2 separate arguments:

$response = $client->savesale(array('sale' => $sale->startsaleresult, 'key' => 'xxxxxxxx')); 

change sale , key whatever wsdl defines them as.


Comments