php 调用 java soap,如何使用SoapClient类进行PHP SOAP调用

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:

Contact ID

Contact Name

General Description

Amount

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());

And here is the output it generates:

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"

}

Say I want to make a call to the FirstFunction with the data:

Contact ID: 100

Contact Name: John

General Description: Barrel of Oil

Amount: 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...

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

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?

UPDATE 2: I've also tried these structures, same error.

$params = array(

array(

"id" => 100,

"name" => "John",

),

"Barrel of Oil",

500,

);

As well as:

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

For this example, I created a .NET sample WebService (WS) with a WebMethod called Function1 expecting the following params:

Function1(Contact Contact, string description, int amount)

Where Contact is just a model that has getters and setters for id and name like in your case.

You can download the .NET sample WS at:

The code.

This is what you need to do at PHP side:

(Tested and working)

// 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);

?>

Testing the whole thing.

If you do print_r($params) you will see the following output, as your WS would expect:

Array ( [Contact] => Contact Object ( [id] => 100 [name] => John )

[description] => Barrel of Oil [amount] => 500 )

When I debugged the .NET sample WS I got the following:

hDwTT.jpg

(As you can see, Contact object is not null nor the other params. That means your request was successfully done from PHP side)

The response from the .NET sample WS was the expected one and this is what I got at PHP side:

object(stdClass)[3] public 'Function1Result' => string 'Detailed

information of your request! id: 100, name: John, description: Barrel

of Oil, amount: 500' (length=98)

Happy Coding!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值