如何使用 SoapClient 类进行 PHP SOAP 调用

2022-08-30 06:48:17

我习惯于编写PHP代码,但不经常使用面向对象的编码。我现在需要与SOAP交互(作为客户端),并且无法获得正确的语法。我有一个 WSDL 文件,它允许我使用 SoapClient 类正确设置新连接。但是,我无法实际做出正确的调用并返回数据。我需要发送以下(简化)数据:

  • 联系人编号
  • 联系人姓名
  • 一般说明

WSDL文档中定义了两个函数,但我只需要一个(下面的“FirstFunction”)。以下是我运行的脚本,用于获取有关可用函数和类型的信息:

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

  • 联络号码: 100
  • 联系人姓名: 约翰
  • 一般描述:石油桶
  • 数量:500

什么是正确的语法?我一直在尝试各种选择,但似乎肥皂结构非常灵活,所以有很多方法可以做到这一点。也无法从手册中找出答案...


更新1:来自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' propertygetTypes()structContact

更新2:我也尝试过这些结构,同样的错误。

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

两种情况下的错误:对象没有“联系人”属性


答案 1

这是您需要做的。

我试图重现这种情况...


  • 对于此示例,我创建了一个 .NET 示例 WebService (WS),其调用期望具有以下参数:WebMethodFunction1

功能1(联系人、字符串描述、整数)

  • 哪里只是一个模型,它有 getter 和 setters for 和 like 在你的情况下。Contactidname

  • 您可以在以下位置下载 .NET 示例 WS:

https://www.dropbox.com/s/6pz1w94a52o5xah/11593623.zip


代码。

这是你需要在PHP端做的事情:

(经过测试和工作)

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

?>

测试整个事情。

  • 如果这样做,您将看到以下输出,正如您的 WS 所期望的那样:print_r($params)

阵列 ( [接触] => 接触对象 ( [id] => 100 [姓名] => 约翰 ) [描述] => 桶油 [数量] => 500 )

  • 当我调试.NET示例WS时,我得到了以下内容:

enter image description here

(如您所见,Contact 对象既不为 null,也不为其他参数。这意味着您的请求已成功从PHP端完成)

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

object(stdClass)[3] 公共 'Function1Result' =>字符串 '您请求的详细信息!id: 100, 名称: 约翰, 描述: 油桶, 数量: 500' (长度=98)



答案 2

您也可以通过以下方式使用 SOAP 服务:

<?php 
//Create the client object
$soapclient = new SoapClient('http://www.webservicex.net/globalweather.asmx?WSDL');

//Use the functions of the client, the params of the function are in 
//the associative array
$params = array('CountryName' => 'Spain', 'CityName' => 'Alicante');
$response = $soapclient->getWeather($params);

var_dump($response);

// Get the Cities By Country
$param = array('CountryName' => 'Spain');
$response = $soapclient->getCitiesByCountry($param);

var_dump($response);

这是一个具有真实服务的示例,当url启动时,它可以工作。

以防万一 http://www.webservicex.net 停机。

下面是另一个使用来自 W3C XML Web Services 的示例 Web 服务的示例,您可以在链接上找到更多信息。

<?php
//Create the client object
$soapclient = new SoapClient('https://www.w3schools.com/xml/tempconvert.asmx?WSDL');

//Use the functions of the client, the params of the function are in
//the associative array
$params = array('Celsius' => '25');
$response = $soapclient->CelsiusToFahrenheit($params);

var_dump($response);

// Get the Celsius degrees from the Farenheit
$param = array('Fahrenheit' => '25');
$response = $soapclient->FahrenheitToCelsius($param);

var_dump($response);

这正在工作并返回转换后的温度值。


推荐