Ver Mensaje Individual
  #19 (permalink)  
Antiguo 06/08/2012, 13:05
rockmandc
 
Fecha de Ingreso: agosto-2012
Mensajes: 1
Antigüedad: 11 años, 8 meses
Puntos: 0
Respuesta: Web service php para obtencion de datos de la base de datos postgresql

Buenas tardes, he estado tratando de implementar el ejemplo pero con una adaptacion a una db en postgresql y se me presenta (Error: Response not of type text/xml: text/html).

Codigo ejemplo

server.php:

require_once('nusoap.php');
function obtenerEstado($id){
$con=pg_connect("host=localhost dbname=prueba user=root password=123" ) or die("Error en la conexion a la base de datos");
$sql="select id, descripcion from estado where id='$id'";
$busqueda=pg_query($con,$sql) ;
if(pg_num_rows($busqueda)!=0){
while( $row = pg_fetch_object ( $busqueda)) {
$respuesta=array('id' => $row->id,
'descripcion' => $row->descripcion);
}
}
return new soapval('return', 'tns:estado', $respuesta);
}
$server = new soap_server();
$miURL='http://localhost/web_service';
$server->configureWSDL('obtenerEstado', $miURL);
$server->wsdl->schemaTargetNamespace=$miURL;
//$server->wsdl->addComplexType('estado','complexType','struct','a ll','',
$server->wsdl->addComplexType('estado','complexType','array','se quence','',
array('id' => array('name' => 'id', 'type' => 'xsd:int'),
'descripcion' => array('name' => 'descripcion', 'type' => 'xsd:string' )
));
$server->register('obtenerEstado',
array('id' => 'xsd:int'),
array('return'=>'tns:estado'),
$miURL);


// Use the request to (try to) invoke the service
if (isset($HTTP_RAW_POST_DATA))
{
$input = $HTTP_RAW_POST_DATA;
}
else
{
$input = implode("\r\n", file('php://input'));
}
$server->service($input);
exit;

?>


cliente.php:

ini_set('soap.wsdl_cache_enabled', '0');
require_once('nusoap.php');
$parametro=$_POST['id'];
$l_oClient = new nusoap_client('http://localhost/web_service/server.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
}
$metodo = 'obtenerEstado';
$l_stResult = $l_oClient->call(
$metodo,
array('id' => $parametro),
"http://localhost/web_service",
"http://localhost/web_service/server.php/$metodo");
// 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>Estado :</h1>'
. '<br>Id: ' . $l_stResult['id']
. '<br>Descripcion ' . $l_stResult['descripcion'];
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($l_oClient->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($l_oClient->response, ENT_QUOTES) . '</pre>';
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($l_oClient->debug_str, ENT_QUOTES) . '</pre>';
}
}
?>

index.php

<html>
<head>
<title>Prueba</title>
<Script languaje="javascript">
function validarFormulario(formulario){
error=false
if(!error&&formulario.idproduc.value==""){
alert('El UserId es obligatorio')
formulario.idproduc.focus()
error=true
}
return !error
}
</script>
</head>
<body>
<center>
<form name="confirmarpar" action="cliente.php" method="POST">
<table>
<tr>
<td>Estado id</td>
<td><input type="text" name="id" 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>