1. Go to this page and download the library: Download byjg/soap-server library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
byjg / soap-server example snippets
use ByJG\SoapServer\Attributes\{SoapService, SoapOperation, SoapParameter};
use ByJG\SoapServer\SoapAttributeParser;
use ByJG\SoapServer\ResponseWriter;
#[SoapService(
serviceName: 'CalculatorService',
namespace: 'http://example.com/calculator',
description: 'A simple calculator web service'
)]
class Calculator
{
#[SoapOperation(description: 'Adds two numbers together')]
public function add(
#[SoapParameter(description: 'First number')] int $a,
#[SoapParameter(description: 'Second number')] int $b
): int {
return $a + $b;
}
#[SoapOperation(description: 'Subtracts second from first')]
public function subtract(int $a, int $b): int
{
return $a - $b;
}
}
// Start the service
$handler = SoapAttributeParser::parse(Calculator::class);
$response = $handler->handle();
ResponseWriter::output($response);
use ByJG\SoapServer\{SoapHandler, SoapOperationConfig, SoapParameterConfig, SoapType};
use ByJG\SoapServer\ResponseWriter;
$addOperation = new SoapOperationConfig();
$addOperation->description = 'Adds two numbers';
$addOperation->args = [
new SoapParameterConfig('a', SoapType::Integer),
new SoapParameterConfig('b', SoapType::Integer)
];
$addOperation->returnType = SoapType::Integer;
$addOperation->executor = function(array $params) {
return $params['a'] + $params['b'];
};
$handler = new SoapHandler(
soapItems: ['add' => $addOperation],
serviceName: 'CalculatorService'
);
$response = $handler->handle();
ResponseWriter::output($response);