(function() {

// MAXB.NET LABS

})();

php

WsCrud – esempio di webservice con nusoap/php

Posted on by Massimiliano Balestrieri | Comments

LINK DEMO:

DOWNLOAD

ISTRUZIONI:

  • Estrarre il pacchetto (htdocs del vostro webserver)
  • configurare il file config/config.php
	define('DB_HOST', '');//esempio localhost
	define('DB_USER', '');//esempio wscrud
	define('DB_NAME', '');//esempio wscrud
	define('DB_PWD', '');//esempio wscrud
	define('WSDL', '');//esempio http://maxb.net/scripts/wscrud/?wsdl
  • creare il database e importare il dump presente in config/db.sql

LIBRERIE DA SCARICARE A PARTE:

  1. adodb_lite
  2. simpletest
  3. nusoap

wscrud

Una volta scaricate ed estratte le librerie dovranno essere inserite nella directory libs come nell’immagine.

Non sono un grande esperto di servizi. Mi è capitato di farne e di usufruirne.

Questa volta ho deciso di estrapolare dall’ultimo lavoro una sorta di scheletro.

Il servizio spiegato:

Inclusioni di librerie:

require_once(dirname(__FILE__) . '/libs/adodb_lite/adodb.inc.php');
require_once(dirname(__FILE__) . '/libs/nusoap-0.7.3/lib/nusoap.php');

require_once(dirname(__FILE__) . '/config/config.php');

require_once(dirname(__FILE__) . '/includes/exception.php');
require_once(dirname(__FILE__) . '/includes/application.php');
require_once(dirname(__FILE__) . '/includes/conn.php');
require_once(dirname(__FILE__) . '/includes/db.php');

Inizializzazione server (vedere il config per capire le costanti):

$server = new nusoap_server();
$server->configureWSDL(APP_NAME_SPACE,APP_TARGET_NAME_SPACE);
$server->wsdl->schemaTargetNamespace = APP_TARGET_NAME_SPACE;

Tipi complessi registrati:


$server->wsdl->addComplexType(
	'record',
	'complexType',
	'struct',
	'all',
	'',
	array(
		'id' => array('name'=>'id','type'=>'xsd:int'),
		'text' => array('name'=>'text','type'=>'xsd:string'),
	)
);

$server->wsdl->addComplexType(
	'array_of_record',
	'complexType',
	'array',
	'',
	'SOAP-ENC:Array',
	array(),
	array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:record[]')),
	'tns:record'
);

I metodi esposti:


$server->register(
	'put_record',
	array('text'=>'xsd:string'),
	array('id'=>'xsd:int'),
	APP_URN,
       APP_URN . '#put_record',
       'rpc',
       'encoded',
	''
);

function put_record($text){
	try{
		$app = new MyApplication();
		return $app->dao->put_record($text);
	}catch(Exception $e){
		return new soap_fault($e->getCode(), 'server', $e->getMessage());
	}
}

$server->register(
	'search_record',
	array(
		'id'=>'xsd:int',
		'text'=>'xsd:string',
	),
	array('return'=>'tns:array_of_record'),
	APP_URN,
    APP_URN . '#search_record',
    'rpc',
    'encoded',
	''
);

function search_record($id, $text){
	try{
		$app = new MyApplication();
		return $app->dao->search_record($id, $text);
	}catch(Exception $e){
		return new soap_fault($e->getCode(), 'server', $e->getMessage());
	}
}

$server->register(
	'get_record',
	array('id'=>'xsd:int'),
	array('return'=>'tns:record'),
	APP_URN,
    APP_URN . '#get_record',
    'rpc',
    'encoded',
	''
);

function get_record($id){
	try{
		$app = new MyApplication();
		return $app->dao->get_record($id);
	}catch(Exception $e){
		return new soap_fault($e->getCode(), 'server', $e->getMessage());
	}
}

$server->register(
	'delete_record',
	array('id'=>'xsd:int'),
	array('return'=>'xsd:boolean'),
	APP_URN,
    APP_URN . '#delete_record',
    'rpc',
    'encoded',
	''
);

function delete_record($id){
	try{
		$app = new MyApplication();
		return $app->dao->delete_record($id);
	}catch(Exception $e){
		return new soap_fault($e->getCode(), 'server', $e->getMessage());
	}
}

$server->register(
	'update_record',
	array('id'=>'xsd:int', 'text' => 'xsd:string'),
	array('return'=>'xsd:boolean'),
	APP_URN,
    APP_URN . '#update_record',
    'rpc',
    'encoded',
	''
);

function update_record($id, $text){
	try{
		$app = new MyApplication();
		return $app->dao->update_record($id, $text);
	}catch(Exception $e){
		return new soap_fault($e->getCode(), 'server', $e->getMessage());
	}
}

L’output del “server”:

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : file_get_contents("php://input");
$server->service($HTTP_RAW_POST_DATA);

Attenzione a quel file_get_contents(“php://input”);
Ho sclerato parecchio per far girare questo servizio sul mio hosting.

IL CLIENT

L’esempio del client VERAMENTE basico senza librerie senza divisioni.
GREZZO. Anche l’html. Giusto il form e il meta per la codifica di invio dei dati.

require_once(dirname(__FILE__) . '/../config/config.php');
error_reporting(E_ALL);
function get_client(){
	require_once(dirname(__FILE__) . '/../libs/nusoap-0.7.3/lib/nusoap.php');
	$url = WSDL;
	$client = new nusoap_client( $url, true );
	return $client;
}

$client = get_client();
$args = array();
$records = $client->call( 'search_record', $args );

if(isset($_POST['save'])){
	$_POST['text'] = stripslashes($_POST['text']);
	$client = get_client();
	if($_POST['id'] == 0){
		$args = array( 'text' => $_POST['text']);
		$result = $client->call( 'put_record', $args );
	}else{
		$args = array( 'id' => $_POST['id'], 'text' => $_POST['text']);
		$result = $client->call( 'update_record', $args );
	}
	header('Location:?do=index');
}
$do = isset($_GET['do']) ? $_GET['do'] : false;
$data = array('id' => 0, 'text' => '');
switch($do){
	case 'edit':
		$client = get_client();
		$args = array( 'id' => $_GET['id']);
		$data = $client->call( 'get_record', $args );
	break;
	case 'delete':
		$client = get_client();
		$args = array(  'id' => $_GET['id']);
		$result = $client->call( 'delete_record', $args );
		header('Location:?do=index');
	break;
}

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<form method="post" action="#">
<p><textarea name="text" id="text"><?php echo $data['text']?></textarea></p>
<p>
<input type="hidden" name="id" id="id" value="<?php echo $data['id'] ?>" />
<input type="submit" name="save" id="save" value="save" />
</p>
</form>
<table>
<thead>
<tr>
	<th>ID</th>
	<th>TEXT</th>
	<th>ACTIONS</th>
<tr>
</thead>
<?php if(isset($records[0])){ ?>
<tbody>
<?php foreach($records as $k => $v){ ?>
<tr>
	<td><?php echo $v['id']?></td>
	<td><?php echo $v['text']?></td>
	<td>
		<a href="?do=edit&amp;amp;id=<?php echo $v['id']?>">edit</a>
		<a href="?do=delete&amp;amp;id=<?php echo $v['id']?>">del</a>
	</td>
<tr>
<?php } ?>
</tbody>
<?php } ?>
<table>

All’interno della directory client troverete due client nativi (estensione soap del php5) : client2.php e client3.php.
Su questo hosting non girano.
Provateli se volete. client3.php usa il metodo “ufficiale” per chiamare i metodi remoti. NON FUNZIONA!

This entry was posted in php and tagged , , , , . Bookmark the permalink.