PHP N-Tier Strategy Step By Step Tutorial - Part 5: After create connection to database at this post, we will process this data. We prepare this data in order to can to send using webservices. We named this: business logic layer.
This practice only work well for php 4. In php 5, we will talk next serial post. Because there is embedded function in php 5. First, copy library of nusoap within www/test/ntier/lib. If you don't understant to place nusoap, please read this post.
Open again file dataacces.php within www/test/ntier. Update like following:
<?
// call library
require_once ( './lib/nusoap.php' ); //nusoap
require_once ('adodb/adodb.inc.php');//adodb
// create instance
$server = new soap_server();
// initialize WSDL support
$server->configureWSDL( 'hello' , 'urn:hellowsdl' );
// place schema at namespace with prefix tns
$server->wsdl->schemaTargetNamespace = 'urn:hellowsdl';
// register method
$server->register('books', // method name
array(), // input parameter
array('return'=>'xsd:array'),
'urn:hellowsdl' , // namespace
'urn:hellowsdl#hello', // soapaction
'rpc', // style
'encoded', // use
'Load list of book to the caller' // documentation
);
// configuration database
define(__databasetype__,'mysql');
define(__server__,'localhost');
define(__user__,'root');
define(__password__,'admin');
define(__database__,'test');
// method
function books(){
$db = ADONewConnection(__databasetype__);
$db->debug = false;
$db->Connect(__server__, __user__, __password__, __database__);
$recordSet = &$db->Execute('select * from books');
if (!$recordSet){
return new soap_fault('Server','',$conn->ErrorMsg());
} else {
while (!$recordSet->EOF) {
$books[] = array('id'=>$recordSet->fields[0],
'title'=>$recordSet->fields[1],
'author'=>$recordSet->fields[2]
);
$recordSet->MoveNext();
}
$recordSet->Close(); #optional
$db->Close();
}
// return value to client
$obj = new soapval('return','array',$books);
return $obj->serialize();
}
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
Then, test it. Open your browser. Point to http://localhost/test/ntier/dataacces.php. You will get like this:
