You do not need as much code to generate a SOAP server or a client. It seems that in the process of copying an example you have left a lot of additional information that you do not need (like the URL reference http://www.greenacorn-websolutions.com/foodservice
).
SOAP server
To create a simple SOAP server (without WSDL), the following lines are enough:
<?php
/* Desarrollamos la clase que responderá las peticiones SOAP */
class predictiveUrlsPreloadService
{
public function getUrls($type)
{
switch ($type) {
case 'GA':
return 'Google Analytics code';
break;
case 'AA':
return 'Adobe Analytics code';
break;
default:
break;
}
}
}
try {
/* Creamos la instancia del servidor SOAP */
/*$server = new SOAPServer(null, [
'uri' => 'http://localhost/tuservicio_soap.php',
]);*/
$server = new SOAPServer(null, [
'uri' => (isset($_SERVER['HTTPS']) ? 'https' : 'http') .
'://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'],
]);
/* Asignamos la clase predictiveUrlsPreloadService como la que responderá al servicio */
$server->setClass('predictiveUrlsPreloadService');
/* Atendemos las peticiones */
$server->handle();
} catch (SOAPFault $e) {
echo $e->faultstring;
}
Use $_SERVER['HTTPS']
, $_SERVER['HTTP_HOST']
and $_SERVER['SCRIPT_NAME']
to make the PHP application portable and that works with the absolute URL that you have.
SOAP Client
The client is much simpler:
<?php
/* $uri = 'http://localhost/tuservicio_soap.php'; */
$uri = (isset($_SERVER['HTTPS']) ? 'https' : 'http') .
'://' . $_SERVER['HTTP_HOST'] . '/servidor_soap.php';
/* Creamos una instancia del cliente SOAP que accederá al servicio anterior*/
$client = new SoapClient(null, [
'location' => $uri,
'uri' => $uri,
]);
/* Llamamos al método getUrls. En modo sin WSDL no podemos usar $client->getUrls(...) */
$resultado = $client->__soapCall('getUrls', [
'tipo' => $_REQUEST['type']
]);
/* Mostramos el resultado */
var_dump($resultado)
Again I have created in $uri
a URL based on the current PHP, assuming that the previous one was called servidor_soap.php
.
JavaScript > REST > SOAP > predictiveUrlsPreloadService :: getUrls
If what you need is to practice with access to different services (REST / JSON and SOAP) then this autonomous PHP script (does not require any additional PHP) shows you an example of how to perform the process:
<?php
/* Detectamos si queremos acceder a los servicios SOAP a través de PATH_INFO (añadir ruta al PHP) */
if (empty($_SERVER['PATH_INFO']) !== true) {
/* Obtenemos la URL absoluta (sin tener en cuenta el puerto) del servidor */
$base = (isset($_SERVER['HTTPS']) ? 'https' : 'http') .
'://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '/servidor';
/* Implementamos el cliente y el servidor en diferentes casos */
switch ($_SERVER['PATH_INFO']) {
case '/servidor':
/* Desarrollamos la clase que responderá las peticiones SOAP */
class predictiveUrlsPreloadService
{
public function getUrls($type)
{
switch ($type) {
case 'GA':
return 'Google Analytics code';
break;
case 'AA':
return 'Adobe Analytics code';
break;
default:
break;
}
}
}
try {
/* Creamos la instancia del servidor SOAP */
$server = new SOAPServer(null, [
'uri' => $base,
]);
/* Asignamos la clase predictiveUrlsPreloadService como la que responderá al servicio */
$server->setClass('predictiveUrlsPreloadService');
/* Atendemos las peticiones */
$server->handle();
} catch (SOAPFault $e) {
echo $e->faultstring;
}
break;
case '/cliente':
/* Creamos una instancia del cliente SOAP que accederá al servicio anterior*/
$client = new SoapClient(null, [
'location' => $base,
'uri' => $base,
]);
/* Llamamos al método getUrls. En modo sin WSDL no podemos usar $client->getUrls(...) */
$resultado = $client->__soapCall('getUrls', [
'tipo' => $_REQUEST['type']
]);
/* Vamos a devolver al javascript datos en JSON */
header('Content-Type: application/json');
die(json_encode([
'resultado' => true,
'datos' => $resultado,
]));
default:
die(json_encode([
'resultado' => false,
'datos' => 'No existe la ruta solicitada',
]));
}
die();
}
?><!DOCTYPE html><html lang="es"><head>
<title>Ejemplo REST/SOAP</title>
<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head><body>
<div class="container" style="padding-top: 20px;">
<div class="alert alert-warning" id="cargando">
<div class="panel-body">Cargando...</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script>
$.ajax({
/*url: '<?= $_SERVER['PHP_SELF'] ?>/clientte',*/
url: '<?= $_SERVER['PHP_SELF'] ?>/cliente',
method: 'get',
data: {
'type' : 'AA',
},
dataType: 'json',
success: function (response) {
console.log(response);
if (response.resultado === true) {
$('#cargando').removeClass('alert-warning').addClass('alert-success');
$('#cargando > div').text('Recibido: ' + response.datos);
} else {
$('#cargando').removeClass('alert-warning').addClass('alert-danger');
$('#cargando > div').text('ERROR: ' + response.datos);
}
},
error: function (response) {
$('#cargando').removeClass('alert-warning').addClass('alert-critical');
$('#cargando > div').text('Se produjo un error');
}
});
</script>
</body></html>
I have used $_SERVER['PHP_INFO']
to facilitate the portability of PHP and make it completely autonomous and self-sufficient, it could have been divided into several PHP.
Javascript > REST > predictiveUrlsPreloadService :: getUrls
But you probably want to give access to the class directly, through REST, without having a SOAP API as an intermediary.
In that case it is enough to implement the following code:
<?php
/* Detectamos si queremos acceder a los servicios SOAP a través de PATH_INFO (añadir ruta al PHP) */
if (!empty($_REQUEST['funcion']) && !empty($_REQUEST['argumentos'])) {
/* Desarrollamos la clase que responderá las peticiones SOAP */
class predictiveUrlsPreloadService
{
public function getUrls($type)
{
switch ($type) {
case 'GA':
return 'Google Analytics code';
break;
case 'AA':
return 'Adobe Analytics code';
break;
default:
break;
}
}
}
$servicio = new predictiveUrlsPreloadService();
if (method_exists($servicio, $_REQUEST['funcion']) === true) {
$resultado = call_user_func([
$servicio,
$_REQUEST['funcion']
], $_REQUEST['argumentos']);
} else {
die(json_encode([
'resultado' => false,
'datos' => 'No existe la función solicitada',
]));
}
header('Content-Type: application/json');
die(json_encode([
'resultado' => true,
'datos' => $resultado,
]));
}
?><!DOCTYPE html><html lang="es"><head>
<title>Ejemplo REST/SOAP</title>
<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head><body>
<div class="container" style="padding-top: 20px;">
<div class="alert alert-warning" id="cargando">
<div class="panel-body">Cargando...</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script>
$.ajax({
/*url: '<?= $_SERVER['PHP_SELF'] ?>/clientte',*/
url: '<?= $_SERVER['PHP_SELF'] ?>',
method: 'get',
data: {
'funcion': 'getUrls',
'argumentos': 'AA',
},
dataType: 'json',
success: function (response) {
console.log(response);
if (response.resultado === true) {
$('#cargando').removeClass('alert-warning').addClass('alert-success');
$('#cargando > div').text('Recibido: ' + response.datos);
} else {
$('#cargando').removeClass('alert-warning').addClass('alert-danger');
$('#cargando > div').text('ERROR: ' + response.datos);
}
},
error: function (response) {
$('#cargando').removeClass('alert-warning').addClass('alert-critical');
$('#cargando > div').text('Se produjo un error');
}
});
</script>
</body></html>
Where I used the functions method_exists
and call_user_func
to know if a method exists in a class and to be able to execute it passing the desired parameters.
Javascript > SOAP > predictiveUrlsPreloadService :: getUrls
Finally, if you want to consume a SOAP web service directly from Javascript you must mount the XML request and receive the XML as I do in this example:
<?php
/* Detectamos si queremos acceder a los servicios SOAP a través de PATH_INFO (añadir ruta al PHP) */
$base = (isset($_SERVER['HTTPS']) ? 'https' : 'http') .
'://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '/servidor';
if (empty($_SERVER['PATH_INFO']) !== true) {
/* Desarrollamos la clase que responderá las peticiones SOAP */
class predictiveUrlsPreloadService
{
public function getUrls($type)
{
switch ($type) {
case 'GA':
return 'Google Analytics code';
break;
case 'AA':
return 'Adobe Analytics code';
break;
default:
break;
}
}
}
try {
/* Creamos la instancia del servidor SOAP */
$server = new SOAPServer(null, [
'uri' => $base,
]);
/* Asignamos la clase predictiveUrlsPreloadService como la que responderá al servicio */
$server->setClass('predictiveUrlsPreloadService');
/* Atendemos las peticiones */
$server->handle();
} catch (SOAPFault $e) {
echo $e->faultstring;
}
die();
}
?><!DOCTYPE html><html lang="es"><head>
<title>Ejemplo REST/SOAP</title>
<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head><body>
<div class="container" style="padding-top: 20px;">
<div class="alert alert-warning" id="cargando">
Cargando...
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script>
/* Función SOAP a llamar y su parámetro */
var funcion = 'getUrls';
var parametro = 'AA';
$.ajax({
/*url: '<?= $_SERVER['PHP_SELF'] ?>/servidor',*/
url: '<?= $base ?>',
method: 'post',
data:
/* Montamos el XML de una petición SOAP con las variables definidas anteriormente */
'<?xml version="1.0" encoding="UTF-8"?>' +
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ' +
'xmlns:ns1="<?= $base ?>" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"' +
'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' +
'<SOAP-ENV:Body>' +
'<ns1:' + funcion + '>' +
'<param0 xsi:type="xsd:string">' + parametro + '</param0>' +
'</ns1:' + funcion + '>' +
'</SOAP-ENV:Body>' +
'</SOAP-ENV:Envelope>',
success: function (response) {
/* Buscamos si existe en la respuesta la respuesta a la función llamada */
if ($(response).find(funcion + 'Response').length === 1) {
$('#cargando').attr('class', 'alert alert-success');
/* Mostramos el contenido en texto del elemento "return" de la respuesta */
$('#cargando').text('Recibido: ' + $(response).find("return").text());
} else {
$('#cargando').attr('class', 'alert alert-danger');
$('#cargando').text('ERROR: ' + response);
}
},
error: function (response) {
/* Si ha habido un error en la llamada SOAP mostramos el error devuelto */
if (response.responseXML !== undefined) {
$('#cargando').attr('class', 'alert alert-danger').text(
'Error SOAP: ' + $(response.responseXML).find("faultstring").text()
);
} else {
$('#cargando').attr('class', 'alert alert-danger').text('Se produjo un error');
}
}
});
</script>
</body></html>