Quote:
kerkyra wrote:
i would like to create a service so that other portals could get data from my website. For example a list of categories, and when one of the click a list of the products it contains. Using an api perhaps?
X-SOAP is ideal for this keryra... X-SOAP uses a plugin system to have the SOAP API Functions.
each plugin/function is represented on the SOAP port as a control device. If you want to retrieve data from X-SOAP i distributed it with some AP i Plugins that allow for database lookups and comparisons.
But you can also write your own function that is specific to your needs.
There are two common types of API, well three there is one in python as well, SOAP, JSON and the python one.
SOAP works with everything, but is more lower kernel than JSON.
All the filenames n /xsoap/plugins are soap functions that come with xsoap, i will examine for you retrieve, which will retrieve data from the database.
/xsoap/plugins/retrieve.php function retrieve_xsd(){
$xsd = array();
$i=0;
$data = array();
$data[] = array("name" => "username", "type" => "string");
$data[] = array("name" => "password", "type" => "string");
$data[] = array("name" => "tablename", "type" => "string");
$data[] = array("name" => "clause", "type" => "string");
$datab=array();
$datab[] = array("name" => "field", "type" => "string");
$data[] = array("items" => array("data" => $datab, "objname" => "data"));
$xsd['request'][$i]['items']['data'] = $data;
$xsd['request'][$i]['items']['objname'] = 'var';
$xsd['response'][] = array("name" => "total_records", "type" => "double");
$data = array();
$data[] = array("name" => "field", "type" => "string");
$data[] = array("name" => "value", "type" => "string");
$i++;
$xsd['response'][$i]['items']['data'] = $data;
$xsd['response'][$i]['items']['objname'] = 'items';
return $xsd;
}
function retrieve_wsdl(){
}
function retrieve_wsdl_service(){
}
// Define the method as a PHP function
function retrieve($var) {
global $xoopsModuleConfig;
if ($xoopsModuleConfig['site_user_auth']==1){
if (!checkright(basename(__FILE__),$var['username'],$var['password']))
return array('ErrNum'=> 9, "ErrDesc" => 'No Permission for plug-in');
}
global $xoopsDB;
if (strlen($var['tablename'])>0) {
$tbl_id = get_tableid($var['tablename']);
} elseif ($var['id']>0) {
$tbl_id = $var['id'];
} else {
return array('ErrNum'=> 2, "ErrDesc" => 'Table Name or Table ID not specified');
}
if (!validate($tbl_id,$var['data'], "allowretrieve")){
return array('ErrNum'=> 4, "ErrDesc" => 'Not all fields are allowed retrieve');
} else {
$sql = "SELECT ";
foreach ($var['data'] as $data){
if ($data['field']=='*')
return array('ErrNum'=> 7, "ErrDesc" => 'Wildcard not accepted');
$sql_b .= "`". $data['field']."`,";
}
if (strlen($var['clause'])>0){
if (strpos(' '.strtolower($var['clause']),'union')>0)
return array('ErrNum'=> 8, "ErrDesc" => 'Union not accepted');
$sql_c .= 'WHERE '.$var['clause'] ."";
}
global $xoopsModuleConfig;
if ($xoopsModuleConfig['site_user_auth']==1){
if (!validateuser($var['username'],$var['password']))
return false;
}
//echo $sql." ".substr($sql_b,0,strlen($str_b)-1)." FROM ".$xoopsDB->prefix(get_tablename($tbl_id))." ".$sql_c;
$rt = $xoopsDB->queryf($sql." ".substr($sql_b,0,strlen($str_b)-1)." FROM ".$xoopsDB->prefix(get_tablename($tbl_id))." ".$sql_c);
if (!$xoopsDB->getRowsNum($rt)){
return array('ErrNum'=> 3, "ErrDesc" => 'No Records Returned from Query');
} else {
$rtn = array();
while($row = $xoopsDB->fetchArray($rt)){
$rdata = array();
foreach ($var['data'] as $data){
$rdata[] = array("fieldname"=> $data['field'], "value"=>$row[$data['field']]);
}
$rtn[] = $rdata;
}
}
return array("total_records" => $xoopsDB->getRowsNum($rt), "items" => $rtn);
}
}
This as all XSOAP Plugins have 2 main functions which all correlate to the filename which has to be unique. there are two legacy functions as well these are:
* retrieve_xsd - concurrent
* retrieve_wsdl - legacy
* retrieve_wsdl_service - legacy
* retrieve -- concurrent
if the api function was called getusers.. then the filename would be getusers.php and the functions would be
* getuser_xsd - concurrent
* getuser_wsdl - legacy
* getuser_wsdl_service - legacy
* getuser -- concurrent
Back to retrieve, see SOAP uses the WSDL Document standard to improve performance.. before 4.25 this was throwing errors which was annoying..
The
WSDL Document which is written to your /modules/xsoap/ path when you activate or deactivate plugins is built from funcname_xsd which contains your SOAP API function schemer, that is the break down of what fields and variable in what tiers of array you will be sending and recieving.
retreve_xsd() function retrieve_xsd(){
$xsd = array();
$i=0;
$data = array();
$data[] = array("name" => "username", "type" => "string");
$data[] = array("name" => "password", "type" => "string");
$data[] = array("name" => "tablename", "type" => "string");
$data[] = array("name" => "clause", "type" => "string");
$datab=array();
$datab[] = array("name" => "field", "type" => "string");
$data[] = array("items" => array("data" => $datab, "objname" => "data"));
$xsd['request'][$i]['items']['data'] = $data;
$xsd['request'][$i]['items']['objname'] = 'var';
$xsd['response'][] = array("name" => "total_records", "type" => "double");
$data = array();
$data[] = array("name" => "field", "type" => "string");
$data[] = array("name" => "value", "type" => "string");
$i++;
$xsd['response'][$i]['items']['data'] = $data;
$xsd['response'][$i]['items']['objname'] = 'items';
return $xsd;
}
This function contains 2 parts the request, that is the question coming to the API Server and the Response -- your webservices answer..
$data = array();
$data[] = array("name" => "username", "type" => "string");
$data[] = array("name" => "password", "type" => "string");
$data[] = array("name" => "tablename", "type" => "string");
$data[] = array("name" => "clause", "type" => "string");
$datab=array();
$datab[] = array("name" => "field", "type" => "string");
$data[] = array("items" => array("data" => $datab, "objname" => "data"));
$xsd['request'][$i]['items']['data'] = $data;
$xsd['request'][$i]['items']['objname'] = 'var';
This part of the request construction, it is a 2 tiered array, but you can tier this as many times as you want making sure you wrap the arrays into each other. as a request this contains, the -- username, password, tablename, clause, then it has an reserved word
items which has an open field name for the object/array with the key name recorded with the reserved word
objnameThis in XML would look a bit like:
<xsoap>
<username>username>
<password>password>
<tablename>tablename>
<clause>clause>
<data>
<array_object_1>
<field>field>
array_object_1>
<array_object_2>
<field>field>
array_object_2>
...
data>
xsoap>
You can see how the object data, is an endless array of field names to retrieve from the database.
Next is the response constructor.. in the funcname_xsd()
$xsd['response'][] = array("name" => "total_records", "type" => "double");
$data = array();
$data[] = array("name" => "field", "type" => "string");
$data[] = array("name" => "value", "type" => "string");
$i++;
$xsd['response'][$i]['items']['data'] = $data;
$xsd['response'][$i]['items']['objname'] = 'data';
This is a result from a SQL Query on your service passed to another service that may not run MySQL or even unix, could be a mainframe or a mobile computer, all part of the cloud.
in XML this piece of code looks like
<xsoap>
<total_records>total_records>
<data>
<record_1>
<array_object_1>
<field>field>
<value>value>
array_object_1>
<array_object_2>
<field>field>
<value>value>
array_object_2>
...
record_1>
<record_2>
<array_object_1>
<field>field>
<value>value>
array_object_1>
<array_object_2>
<field>field>
<value>value>
array_object_2>
...
record_2>
...
data>
xsoap>
Essentially you will want to write your own plugin kerkyra, for a simple thing like a category list from your web service.
There is a wiki on X-SOAP:
https://xoops.org/modules/mediawiki/index.php?title=X-Soap&curid=1095&oldid=4023#retrieve_-_Requestfinally the function which is called when someone dials up your SOAP server. This is the same as the filename without the php extension or period.
When someone calls his function, the request is passed to it, through var or however you have constructed the inbound call and the
return function passes the result (arrays are best0 back to the computer that placed the query on the SOAP Service..