2013-07-19 51 views
7

Tôi đang truy cập một dịch vụ web bằng cách sử dụng SOAP và PHP. Tôi có thể kết nối với một số chức năng thông qua API webservice. Nhưng một trong những eludes tôi. Tôi có một ví dụ nhưng nó là trong C#.Chuyển đổi C# sang PHP

Tôi đã cố gắng mô phỏng ví dụ trong PHP mà không có may mắn.

Tôi đã đính kèm mã C# và nỗ lực của tôi trong PHP. Ngoài ra thông báo lỗi cũng được bao gồm.

C# code nhuận

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 a return of goods. 
     line1.LineAmount = 600; //The normal amount of the goods. 
     line1.EDBNumber = 1; //unique key in our system. You have this if you got the articles by our service. 
     line1.DiscountAmount = -100; //the discount. 

     //line2 comes here.... 

     //add the lines to the sale 
     sale.ItemLines = new ItemLine[] { line1 }; 

     //The payment lines 
     //1 with cash, and one with the change. the sale MUST sum to zero (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 the paymentlines 
     sale.PaymentLines = new PaymentLine[]{cash,change}; 

     //submit the sale. 
     Status response = client.SaveSale(sale, yourKey); 
     Console.WriteLine(string.Format("got response from save sale saved: {0}, message: {1}",response.Saved,response.Message)); 
     Console.ReadLine(); 
    } 
} 

mã PHP

$client = new SoapClient("http://xxx.xxxxx.xxxx/xxxxxxx.svc?wsdl", array("trace" => 1, "connection_timeout" => 500)); 

// Initialize sale 
// $client is a valid SOAP connection 
// That has been setup earlier 
$Sale = $client->StartSale(); 

// Output what 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 gì webservice khi kết nối

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 
) 

Thông báo lỗi

[previous:Exception:private] => 
    [faultstring] => End element 'Body' from namespace 'http://schemas.xmlsoap.org/soap/envelope/' expected. Found element 'param1' from namespace ''. Line 2, position 149. 
    [faultcode] => a:InternalServiceFault 
    [detail] => stdClass Object 
     (
      [ExceptionDetail] => stdClass Object 
       (
        [HelpLink] => 
        [InnerException] => 
        [Message] => End element 'Body' from namespace 'http://schemas.xmlsoap.org/soap/envelope/' expected. Found element 'param1' from namespace ''. Line 2, position 149. 
        [StackTrace] => at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3) 
    at System.Xml.XmlExceptionHelper.ThrowEndElementExpected(XmlDictionaryReader reader, String localName, String ns) 
    at System.Xml.XmlBaseReader.ReadEndElement() 
    at System.ServiceModel.Channels.Message.ReadFromBodyContentsToEnd(XmlDictionaryReader reader, EnvelopeVersion envelopeVersion) 
    at System.ServiceModel.Channels.Message.ReadFromBodyContentsToEnd(XmlDictionaryReader reader) 
    at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest) 
    at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters) 
    at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) 
    at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) 
    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) 
    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) 
    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) 
    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) 
    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) 
    at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet) 
        [Type] => System.Xml.XmlException 
       ) 

     ) 
+1

$ khách hàng là gì? – DevZer0

+0

Kết nối với webservice bằng SOAP. Đã chỉnh sửa câu hỏi của tôi chỉ trong trường hợp. – Cudos

Trả lời

9

mảng của bạn không nhìn đúng đắn. ItemLinesPaymentLines phải là mảng thay vì đối tượng chứa mảng.

Mặc dù StartSale trả về chúng làm đối tượng, chúng vẫn phải là mảng cho cuộc gọi đến SaveSale. Lý do chúng được trả về vì các đối tượng là một điều kỳ quặc của SoapClient PHP. Điều gì xảy ra là, nếu chỉ có một phần tử trong mảng, SoapClient sẽ cho bạn một đối tượng với các thuộc tính của nó được thiết lập như là các thuộc tính phần tử đơn. Tuy nhiên, nếu có nhiều hơn một phần tử trong mảng, bạn sẽ nhận được một mảng các đối tượng thay vào đó, và vì vậy một cấp sâu hơn kết quả khi chỉ có một phần tử duy nhất. Vì vậy, bởi vì chúng được trả về không có các phần tử từ StartSale, chúng được đưa ra dưới dạng đối tượng thay vì một mảng trống.

... 
... 
$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); 

Cũng tùy thuộc vào những gì các WSDL trông giống như, bạn có thể cần phải vượt qua một mảng kết hợp như các thông số, với các phím như salekey (theo WSDL), thay vì đi qua hai đối số riêng biệt:

$response = $client->SaveSale(array('sale' => $Sale->StartSaleResult, 'key' => 'xxxxxxxx')); 

Thay đổi salekey thành bất cứ điều gì WSDL định nghĩa chúng là.

+0

Tuyệt vời! Tôi làm việc như một say mê, Cảm ơn một bó! – Cudos

+0

Không vấn đề gì .. :) – MrCode