tags:

views:

51

answers:

2

Hello,

I am trying to develop a very simple SOAP server and Client in PHP. The goal is to receive content from a remote XML-document as the source.

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. 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? Am I on the right track, is this a SOAP setup?

  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!

A: 

The problem is not a SOAP server problem, it is about XML access

Assuming you XML contains the same data as the array quoted in exemple, and you can get simpleXML on your server

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

In your example it has one big advantage, you can search result directly inside the XML using Xpath, instead of using array.

Benoit
Thanks! This looks interesting. So the $items above is now an ordinary array? Does the rest of the code look fine (I am a novice in SOAP)?
A: 

I have changed the setup to this but there are no data sent out:

 function objectsIntoArray($arrObjData, $arrSkipIndices = array()) {
      $arrData = array();

      // if input is object, convert into array
      if (is_object($arrObjData)) {
          $arrObjData = get_object_vars($arrObjData);
      }

      if (is_array($arrObjData)) {
          foreach ($arrObjData as $index => $value) {
              if (is_object($value) || is_array($value)) {
                  $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
              }
              if (in_array($index, $arrSkipIndices)) {
                  continue;
              }
              $arrData[$index] = $value;
          }
      }
      return $arrData;
  }

  function getItemCount($upc) {

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

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

    // Set the XML source

    $xmlUrl = "source.xml";

  // Get the XML data

  $xmlStr = file_get_contents($xmlUrl);

    // Load the object into variable

    $xmlObj = simplexml_load_string($xmlStr);

    // Turn into an array

  $items = objectsIntoArray($xmlObj);

    // Return the requested value

    return $items[$upc];

}

This is the content of my XML file. Is it the same as the array (format)?

<12345>5</12345>
<19283>100</19283>
<23489>234</23489>

Please help!