Reputation: 2304
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!
Upvotes: 0
Views: 1756
Reputation: 3598
The problem is not a SOAP server problem, it is about XML access.
Assuming your XML contains the same data as the array quoted in example, 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.
Upvotes: 2
Reputation: 791
for better use you can try soap client like http://sourceforge.net/projects/nusoap/ for better flexibility.
Upvotes: 0