Ver Mensaje Individual
  #3 (permalink)  
Antiguo 16/04/2012, 18:26
julianrb90
 
Fecha de Ingreso: marzo-2012
Mensajes: 21
Antigüedad: 12 años, 1 mes
Puntos: 1
Respuesta: Web service php para obtencion de datos de la base de datos postgresql

Cita:
Iniciado por maycolalvarez Ver Mensaje
¿y tienes algo hecho, intentaste hacer algo, buscaste documentación?, aquí en el foro se incentiva a los usuarios que se esfuerzan, no simplemente le damos el código u ejemplos a la medida, si tiene un problema o duda concreto posteelo, en la web puede hallar suficiente documentación para hacer lo que quiere.
Claro que he desarrollado algo pero no lo presente porque quiero desarrollar conocimiento desde cero.

Si algo aqui esta el codigo.

PHP ws.php

Cita:
<?php
require_once('lib/nusoap.php');
$server = new soap_server();
$miURL='http://localhost:9090/Prueba';
$server->configureWSDL('obtenerProducto', $miURL);
$server->wsdl->schemaTargetNamespace=$miURL;
$server->wsdl->addComplexType('producto','complexType','struct', 'all','',
array(
'idProducto' => array('name' => 'idProducto', 'type' => 'xsd:int'),
'titulo' => array('name' => 'titulo', 'type' => 'xsd:string'),
'descripcion' => array('name' => 'descripcion', 'type' => 'xsd:string' ),
'precio' => array('name' => 'precio', 'type' => 'xsd:int' ),
));

$server->register('obtenerProducto',
array('idProducto' => 'xsd:int'),
array('return'=>'tns:producto'),
$miURL);

function obtenerProducto($id){
$con=pg_connect("host=localhost dbname=prueba user=admin password=123" ) or die("Error en la conexion a la base de datos");
$sql = "select idProducto, titulo, descripcion, precio from producto where idProducto = $id ;";
$busqueda=pg_query($con,$sql) ;
if(pg_num_rows($busqueda)!=0){
while( $row = pg_fetch_object ( $busqueda)) {
$respuesta=array('idProducto' => $row->idProducto,
'titulo' => $row->titulo,
'descripcion' => $row->descripcion,
'precio' => $row->precio);
}
}
return new soapval('return', 'tns:producto', $respuesta);


}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
PHP cliente.php

Cita:
<?php
require_once('lib/nusoap.php');
$l_oClient = new nusoap_client('http://localhost:9090/Prueba/ws.php?wsdl','wsdl');
$err = $l_oClient->getError();
if ($err) {
// Display the error
echo '<p><b>Constructor error: ' . $err . '</b></p>';
// At this point, you know the call that follows will fail
}
$metodoALlamar = 'obtenerProducto';
$error = $l_oClient->getError();
if ($error) {
echo '<pre style="color: red">' . $error . '</pre>';
echo '<p style="color:red;'>htmlspecialchars($l_oClient->getDebug(), ENT_QUOTES).'</p>';
die();
}

$parametro = $_POST['idProducto'];


$l_stResult = $l_oClient->call(
$metodoALlamar,
array('id' => $parametro),
"uri:http://localhost:9090/Prueba/ws.php",
"uri:http://localhost:9090/Prueba/ws.php/$metodoALlamar");

// Verificacion que los parametros estan ok, y si lo estan. mostrar rta.
if ($l_oClient->fault) {
echo '<b>Error: ';
print_r($l_stResult);
echo '</b>';
} else {
$error = $l_oClient->getError();
if ($error) {
echo '<b style="color: red">Error: ' . $error . '</b>';
} else {
print '<h1>Producto :</h1>'
. '<br>Id Producto: ' . $l_stResult['idProducto']
. '<br>Titulo : ' . $l_stResult['titulo']
. '<br>Descripcion ' . $l_stResult['descripcion']
. '<br>Precio ' . $l_stResult['precio'];


echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($_oClient->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($_oClient->response, ENT_QUOTES) . '</pre>';
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($_oClient->debug_str, ENT_QUOTES) . '</pre>';

}
}

?>
PHP index.php

Cita:
<html>
<head>
<title>Prueba</title>
<Script languaje="javascript">
function validarFormulario(formulario){
error=false
if(!error&&formulario.idProducto.value==""){
alert('El UserId es obligatorio')
formulario.userid.focus()
error=true
}
return !error
}
</script>
</head>
<body>
<center>
<form name="confirmarpar" action="cliente.php" method="POST">
<table>
<tr>
<td>UserID</td>
<td><input type="text" name="idProducto" size="20" maxlength="20"/></td>

</tr>
<tr>
<td><input type="submit" name="enviar" value="Enviar" onClick="return validarFormulario(confirmarpar)"/></td>
<td><input type="reset" name="borrar" value="Borrar"/></td>
</tr>
</table>

</form>
</center>
</body>

</html>