简体   繁体   English

PHP中的SOAP服务器和客户端

[英]SOAP Server and Client in PHP

I am trying to develop a very simple SOAP server and Client in PHP. 我正在尝试用PHP开发一个非常简单的SOAP服务器和客户端。 The goal is to receive content from a remote XML-document as the source. 目标是从远程XML文档接收内容作为源。

This is what I have done so far, I need help how to extract data from an XML file instead, as it is now, from an ordinary array. 到目前为止,这是我所做的事情,我需要帮助,如何从XML文件中提取数据,而不是像现在这样从普通数组中提取数据。 This is the function found in inventory_functions.php that is fetching from an array, how can it be changed to fetch from the XML file instead? 这是从数组中获取的ventory_functions.php中的函数,如何将其更改为从XML文件中获取呢? Am I on the right track, is this a SOAP setup? 我是否走对了,这是SOAP设置吗?

  function getItemCount($upc) {

    // In reality, this data would be coming from a database

    $items = array('12345'=>5,'19283'=>100,'23489'=>'234');

    // Return the requested value

    return $items[$upc];

}

This is the code for the server: 这是服务器的代码:

  // Load the database

  require 'inventory_functions.php';

  // Turn off WSDL cache

  ini_set("soap.wsdl_cache_enabled", "0");

  // Create a new SoapServer object with inventory.wsdl

  $server = new SoapServer("inventory.wsdl");

  // Register the getItemCount function

  $server->addFunction("getItemCount");

  // Start the handle

  $server->handle();

This is the code for the client: 这是客户端的代码:

// Turn off WSDL cache

ini_set("soap.wsdl_cache_enabled", "0");

// Create a new SOAPClient object

$client = new SoapClient("inventory.wsdl");

// Get the value for the function getItemCount with the ID of 12345

$getItemCount = $client->getItemCount('12345');

// Print the result

echo ($getItemCount);

Please help! 请帮忙!

The problem is not a SOAP server problem, it is about XML access. 问题不是SOAP服务器问题,而是关于XML访问的问题。

Assuming your XML contains the same data as the array quoted in example, and you can get simpleXML on your server: 假设您的XML包含与示例中引用的数组相同的数据,并且您可以在服务器上获取simpleXML

//load your xml file into $xmlStr, with file_get_contents() or whatever.
$xmlObj = simplexml_load_string($xmlStr);
$items = objectsIntoArray($xmlObj);

You can also use DomDocument instead, it has a DOM API, so if you're familiar with HTML DOM it will be easier. 您也可以改用DomDocument ,它具有DOM API,因此,如果您熟悉HTML DOM,它将更加容易。

In your example it has one big advantage, you can search result directly inside the XML using Xpath, instead of using array. 在您的示例中,它具有一大优势,您可以使用Xpath而不是使用数组直接在XML内搜索结果。

为了更好地使用,您可以尝试使用诸如http://sourceforge.net/projects/nusoap/之类的soap客户端,以获得更好的灵活性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM