Skip to content

Instantly share code, notes, and snippets.

@marcosfreitas
Last active January 29, 2021 12:24
Show Gist options
  • Save marcosfreitas/a642c1f1a82fd9ff8cf1b30b8274080f to your computer and use it in GitHub Desktop.
Save marcosfreitas/a642c1f1a82fd9ff8cf1b30b8274080f to your computer and use it in GitHub Desktop.
<?php
namespace App;
/**
* Esta classe é apenas um Wrapper para a classe SoapClient e realiza as requisições e recebe as respostas de forma padrão.
*/
class Soap {
public
$__client,
$__server;
private
$wsdl_url;
private
$__server_name = 'cbl',
$__namespace = APP_URL . 'wsdl/index.php',
$__wsdl_valid_xml = APP_URL . 'wsdl/wsdl.xml';
public function __construct($new_wsdl_url = null) {
if (null === WSDL_URL && empty($new_wsdl_url)) {
throw new \Exception("A URL do WEBService não foi definida.", 1);
}
if (!empty($new_wsdl_url)) {
$this->wsdl_url = $new_wsdl_url;
} else {
$this->wsdl_url = WSDL_URL;
}
}
/**
* Client
* -- for self test
*/
public function newClient() {
//$this->__client = new \SoapClient($this->wsdl_url, array('cache_wsdl' => WSDL_CACHE_NONE, 'trace' => 0));
$this->__client = new \nusoap_client($this->wsdl_url, 'wsdl');
return $this->__client;
}
public function request($function = null, $params = null) {
if (!empty($function)) {
//return $this->__client->__soapCall($function, $params);
return $this->__client->call($function, $params);
}
throw new \Exception("Defina um método correto para ser chamado.", 1);
}
/**
* Configure a new WSL Server
*/
public function configureServer() {
$this->__server = new \nusoap_server();
$this->__server->soap_defencoding = 'ISO-8859-1';
$this->__server->decode_utf8 = true;
$this->__server->configureWSDL($this->__server_name, $this->__namespace);
$this->__server->wsdl->schemaTargetNamespace = $this->__namespace;
return $this->__server;
}
/**
* Register the functions as services of the webservice. First, call 'configureServer()', after, call 'serviceRegister()' to any module that you want to attach to Webservice WSDL.
* @param array $params The options to configure the module.
* Expected:
* - string function_name
* - array function_params
* - array function_return_values
* - string soap_action
* - string description
*/
public function serviceRegister($params = array()) {
if (empty($params)) {
throw new \Exception("Os parâmetros para registrar o método não foram definidos.", 1);
}
$this->__server->register(
$params['function_name'],
$params['function_params'],
$params['function_return_values'],
$this->__namespace,
$this->__namespace.'#'.$params['soap_action'],
'rpc',
'encoded',
$params['description']
);
}
/**
* "Start the server" already configured capturing the data received from a Client call.
* @return [type] [description]
*/
public function startServer() {
# HTTP_RAW_POST_DATA are ALL THE POST DATA received that are not treated.
$__http_raw_post_data = file_get_contents("php://input");
$this->__server->service($__http_raw_post_data);
}
public function debug() {
$_request = $this->__client->request;
$_response = $this->__client->response;
echo '<h2>Request</h2>';
echo '<pre>' .htmlspecialchars($_request, ENT_QUOTES).'</pre>';
echo '<h2>Response</h2>';
echo '<pre>' .htmlspecialchars($_response, ENT_QUOTES).'</pre>';
$error = $this->__client->getError();
if ($error) {
echo "<h2>Constructor error</h2><pre>" . $error . "</pre>";
}
if ($this->__client->fault) {
echo "<h2>Fault</h2><pre>";
echo ($response);
echo "</pre>";
}
else {
$error = $this->__client->getError();
if ($error) {
echo "<h2>Error</h2><pre>" . $error . "</pre>";
}
if (isset($response) && !empty($response)){
echo "<h2>Respond</h2><pre>";
print_r ($response);
echo "</pre>";
}
}
}
public function __destruct() {
//unset($this->__client);
//unset($this->__server);
}
}
@basim-usman
Copy link

what is WSDL_URL in that ?

@marcosfreitas
Copy link
Author

That is the SOAP endpoint.

@basim-usman
Copy link

ok

@marcosfreitas
Copy link
Author

This is an old project, but I've just updated the source for this latest version. This is just a wrapper class for using this package installed by composer: https://github.com/codecasts/nusoap-php7. This is a script that I used to test: https://gist.github.com/marcosfreitas/e0b18b94309ee02323a6a5fa066d8e8f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment