問題描述
我習慣于編寫 PHP 代碼,但不經常使用面向對象的編碼.我現在需要與 SOAP(作為客戶端)交互,但無法正確使用語法.我有一個 WSDL 文件,它允許我使用 SoapClient 類正確設置新連接.但是,我實際上無法進行正確的調用并返回數據.我需要發送以下(簡化的)數據:
I'm used to writing PHP code, but do not often use Object-Oriented coding. I now need to interact with SOAP (as a client) and am not able to get the syntax right. I've got a WSDL file which allows me to properly set up a new connection using the SoapClient class. However, I'm unable to actually make the right call and get data returned. I need to send the following (simplified) data:
- 聯系人 ID
- 聯系人姓名
- 一般說明
- 金額
WSDL 文檔中定義了兩個函數,但我只需要一個(下面的FirstFunction").這是我運行的腳本以獲取有關可用函數和類型的信息:
There are two functions defined in the WSDL document, but I only need one ("FirstFunction" below). Here is the script I run to get information on the available functions and types:
$client = new SoapClient("http://example.com/webservices?wsdl");
var_dump($client->__getFunctions());
var_dump($client->__getTypes());
這是它生成的輸出:
array(
[0] => "FirstFunction Function1(FirstFunction $parameters)",
[1] => "SecondFunction Function2(SecondFunction $parameters)",
);
array(
[0] => struct Contact {
id id;
name name;
}
[1] => string "string description"
[2] => string "int amount"
}
假設我想用數據調用 FirstFunction:
Say I want to make a call to the FirstFunction with the data:
- 聯系人 ID:100
- 聯系人姓名:約翰
- 一般描述:一桶油
- 數量:500
正確的語法是什么?我一直在嘗試各種選擇,但看起來肥皂結構非常靈活,因此有很多方法可以做到這一點.手冊上也查不到...
What would be the right syntax? I've been trying all sorts of options but it appears the soap structure is quite flexible so there are very many ways of doing this. Couldn't figure it out from the manual either...
更新 1:來自 MMK 的嘗試樣本:
UPDATE 1: tried sample from MMK:
$client = new SoapClient("http://example.com/webservices?wsdl");
$params = array(
"id" => 100,
"name" => "John",
"description" => "Barrel of Oil",
"amount" => 500,
);
$response = $client->__soapCall("Function1", array($params));
但我得到了這樣的回應:Object has no 'Contact' property
.正如你在 getTypes()
的輸出中看到的,有一個 struct
叫做 Contact
,所以我想我需要以某種方式明確我的參數包括聯系人數據,但問題是:如何?
But I get this response: Object has no 'Contact' property
. As you can see in the output of getTypes()
, there is a struct
called Contact
, so I guess I somehow need to make clear my parameters include the Contact data, but the question is: how?
更新 2:我也試過這些結構,同樣的錯誤.
UPDATE 2: I've also tried these structures, same error.
$params = array(
array(
"id" => 100,
"name" => "John",
),
"Barrel of Oil",
500,
);
還有:
$params = array(
"Contact" => array(
"id" => 100,
"name" => "John",
),
"description" => "Barrel of Oil",
"amount" => 500,
);
兩種情況下的錯誤:對象沒有聯系人"屬性`
Error in both cases: Object has no 'Contact' property`
推薦答案
這是你需要做的.
我試圖重現這種情況...
This is what you need to do.
I tried to recreate the situation...
- 對于本示例,我創建了一個 .NET 示例 WebService (WS),其中包含一個名為
Function1
的WebMethod
,需要以下參數:
- For this example, I created a .NET sample WebService (WS) with a
WebMethod
calledFunction1
expecting the following params:
Function1(Contact Contact, string description, int amount)
Function1(Contact Contact, string description, int amount)
其中
Contact
只是一個具有id
和name
的 getter 和 setter 的模型,就像您的情況一樣.Where
Contact
is just a model that has getters and setters forid
andname
like in your case.您可以在以下位置下載 .NET 示例 WS:
You can download the .NET sample WS at:
https://www.dropbox.com/s/6pz1w94a52o5xah/11593623.zip
這是你在 PHP 端需要做的:
This is what you need to do at PHP side:
(經過測試和工作)
<?php // Create Contact class class Contact { public function __construct($id, $name) { $this->id = $id; $this->name = $name; } } // Initialize WS with the WSDL $client = new SoapClient("http://localhost:10139/Service1.asmx?wsdl"); // Create Contact obj $contact = new Contact(100, "John"); // Set request params $params = array( "Contact" => $contact, "description" => "Barrel of Oil", "amount" => 500, ); // Invoke WS method (Function1) with the request params $response = $client->__soapCall("Function1", array($params)); // Print WS response var_dump($response); ?>
測試整個事情.
- 如果您執行
print_r($params)
,您將看到以下輸出,正如您的 WS 所期望的: - If you do
print_r($params)
you will see the following output, as your WS would expect: - 當我調試 .NET 示例 WS 時,我得到以下信息:
- 來自 .NET 示例 WS 的響應是預期的響應,這就是我在 PHP 方面得到的響應:
Testing the whole thing.
Array ( [Contact] => Contact Object ( [id] => 100 [name] => John )[說明] =>每桶油 [數量] =>500)
Array ( [Contact] => Contact Object ( [id] => 100 [name] => John ) [description] => Barrel of Oil [amount] => 500 )
(如您所見,
Contact
對象既不是null
也不是其他參數.這意味著您的請求已從 PHP 端成功完成)(As you can see,
Contact
object is notnull
nor the other params. That means your request was successfully done from PHP side)object(stdClass)[3] public 'Function1Result' =>字符串 '詳細您的請求信息!id:100,姓名:John,描述:桶油,數量:500'(長度=98)
object(stdClass)[3] public 'Function1Result' => string 'Detailed information of your request! id: 100, name: John, description: Barrel of Oil, amount: 500' (length=98)
這篇關于如何使用 SoapClient 類進行 PHP SOAP 調用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!- 如果您執行