Revision [1479]

This is an old revision of APIDocumentation made by PhilDaintree on 2009-05-03 21:59:34.

 

webERP API


This is the for dummies version - since that is the standpoint I am looking at it from!!

The API uses the phpxmlrpc class from Useful Inc originally developed by Edd Dumbill

(Why didn't you use the PHP built in stuff Tim? I believe it was based on this class?)

xml-rpc is a protocl to use XML to make RPC - remote procedure calls.

Simply put the XML_RPC call is XML that contains the method of the remote procedure call and any parameters and their data types and is sent over http as a POST to the XML-RPC server - the server returns an XML payload containing the results of the call.

The clever thing about XML-RPC is that it is the simplest protocol around for doing web-services. The newer and MUCH more complex SOAP - Simple Object Access Protocol - is quite involved and complicated and therefore doesn't belong in webERP!!

In fact this XML-RPC "Server" in webERP is just the script:

http://www.yourdomain.com/webERP/api/api_xml-rpc.php


there is no daemon required to field calls to the "server" it is just a script that is http posted to by the XML-RPC call sending the XML encoded method to be run together with the necessary parameters to the webERP API - the server script runs Tim's php functions exposed by the xml-rpc methods and returns the XML-RPC response as an XML payload. The phpxmlrpc class does the packaging converting the PHP variables and arrays to the XML required for the XML-RPC call and also has the functions to convert the XML response into something useable in PHP without having to write the XML parsing routines.

I read a how-to on XML-RPC with PHP which contains a little more detail of what is going on and was a useful primer for the concepts.

Tim's API help which itself is produced by an xml-rpc call to the API using the system.listMethods method (I think this is a phpxmlrpc method - not a webERP API method) - he then uses another system xml-rpc method of phpxmlrpc class to return the details of each method's parameters required. So the help file not only documents each of the API methods it is itself and illustration of how the API can be used!!

I have had a go at an example xml-rpc client script to retrieve the stock quantities of an item using the doc/Manual/ManualAPIFunctions.php as a template.


<?php
echo '<html><head>Test Tims webERP API</head><body>';

//the xmlrpc class can output some funny warnings so make sure notices are turned off
error_reporting (E_ALL & ~E_NOTICE);

//you need to include the phpxmlrpc class - see link above - copy the whole directory structure of the class over to your client application
include ("xmlrpc/lib/xmlrpc.inc");

//if your webERP install is on a server at http://www.yourdomain.com/webERP
$ServerURL = "http://www.yourdomain.com/webERP/api/api_xml-rpc.php";
$DebugLevel = 0; //Set to 0,1, or 2 with 2 being the highest level of debug info

$Parameters = array();

/*The trap for me was that each parameter needs to be run through xmlrpcval()  - to create the necessary xml required for the rpc call
if one of the parameters required is an array then it needs to be processing into xml for the rpc call through php_xmlrpc_encode()*/


$Parameters['StockID'] = xmlrpcval('DVD-TOPGUN'); //the stockid of the item we wish to know the balance for
//assuming the demo username and password will work !
$Parameters['Username'] = xmlrpcval('admin');
$Parameters['Password'] = xmlrpcval('weberp');

$msg = new xmlrpcmsg("weberp.xmlrpc_GetStockBalance", $Parameters);

$client = new xmlrpc_client($ServerURL);
$client->setDebug($DebugLevel);
$response = $client->send($msg);
$answer = php_xmlrpc_decode($response->value());
if ($answer[0]!=0){ //then the API returned some errors need to figure out what went wrong
   
//need to figure out how to return all the error descriptions associated with the codes

} else { //all went well the returned data is in $answer[1]
//answer will be an array of the locations and quantity on hand for DVD_TOPGUN so we need to run through the array to print out
for ($i=0; $i<sizeof($answer[1]);$i++) {
  echo '<br>' . $answer[1][$i]['loccode'] . ' has ' . $answer[1][$i]['quantity'] . ' on hand';
}  
echo '</body></html>';
?>


Tim has prepared some example scripts showing how a number of the API XML-RPC functions are called - these scripts can be put on a web-server outside a webERP installation - all you need to do is edit the config.inc file to give the system your webERP username and password and the URL of your webERP installation you wish to connect to.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki