如何使用SoapClient类进行PHP SOAP调用 [英] How to make a PHP SOAP call using the SoapClient class

查看:1001
本文介绍了如何使用SoapClient类进行PHP SOAP调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于编写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
  • 联系人姓名:John
  • 概述:每桶石油
  • 金额: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()的输出中看到的,有一个名为Contactstruct,所以我想我需要以某种方式弄清楚我的参数包括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...

  • 在此示例中,我创建了一个名为Function1的.c示例WebService(WS),期望以下参数:
  • For this example, I created a .NET sample WebService (WS) with a WebMethod called Function1 expecting the following params:

Function1(联系人,字符串说明,整数值)

Function1(Contact Contact, string description, int amount)

  • Contact只是一个具有idname的getter和setter方法的模型,就像您的情况一样.

    • Where Contact is just a model that has getters and setters for id and name 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所期望的:

      • Testing the whole thing.

        • If you do print_r($params) you will see the following output, as your WS would expect:
        • Array([Contact] => Contact对象([id] => 100 [name] => John) [description] =>每桶石油[amount] => 500)

          Array ( [Contact] => Contact Object ( [id] => 100 [name] => John ) [description] => Barrel of Oil [amount] => 500 )

          • 调试.NET示例WS时,我得到以下信息:
          • (如您所见,Contact对象既不是null也不是其他参数.这意味着您的请求已从PHP端成功完成)

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

            • .NET示例WS的响应是预期的响应,这就是我在PHP方面得到的:

            object(stdClass)[3] public'Function1Result'=>字符串'Detailed 您要求的信息! id:100,名字:约翰,描述:桶 油,量: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)


            快乐编码!


            Happy Coding!

            这篇关于如何使用SoapClient类进行PHP SOAP调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆