Usage: configure a connection

PHP/SAP supports SAP remote system connection types A and B.

The configuration classes implement the JsonSerializable interface . The configuration can therefore be stored as JSON by using json_encode on the configuration object.

Set configuration parameters and JSON encode them

All possible configuration parameters are methods of the respective configuration type. That way you don’t need to know their exact names, as long as your editor supports auto-completion for classes loaded via composer.

<?php
use phpsap\classes\Config\ConfigTypeA;
//Create an empty configuration object of type A.
$config = new ConfigTypeA();
//ConfigTypeA and ConfigTypeB common methods are in the CommonTrait
$config->setAshost('sap.example.com')
       ->setSysnr('999')
       ->setClient('001')
       ->setUser('username')
       ->setPasswd('password');
//JSON encode the configuration.
echo json_encode($config, JSON_PRETTY_PRINT) . PHP_EOL;

Output

{
  "ashost": "sap.example.com",
  "sysnr": "999",
  "client": "001",
  "user": "username",
  "passwd": "password"
}

Get a configuration from JSON

Of course the JSON encoded configuration is not a dead end. The trait \phpsap\classes\Config\Traits\JsonDecodeTrait provides the method jsonDecode($string) for the classes \phpsap\classes\Config\ConfigTypeA and \phpsap\classes\Config\ConfigTypeB which can be used to directly decode a JSON encoded configuration into a configuration class.

<?php
use phpsap\classes\Config\ConfigTypeB;
use phpsap\classes\Config\ConfigTypeA;
//Let's assume the JSON encoded config from before.
$jsonConfig = '{
  "ashost": "sap.example.com",
  "sysnr": "999",
  "client": "001",
  "user": "username",
  "passwd": "password"
}';
/**
 * Let the JsonDecodeTrait determine the type of configuration
 * (A or B) of your formerly encoded JSON.
 */
$configA = ConfigTypeB::jsonDecode($jsonConfig);
/**
 * You can always create an instance of your config
 * using already decoded arrays.
 */
$anotherConfigA = new ConfigTypeA(json_decode($jsonConfig, true));

Set configuration parameters.

You just learned that the constructor of the configuration classes accepts an array. Now nothing’s holding you back to manually build the same array.

<?php
use phpsap\classes\Config\ConfigTypeA;
use phpsap\interfaces\Config\IConfigTypeA;
use phpsap\interfaces\Config\IConfiguration;

$config = new ConfigTypeA([
    IConfigTypeA::JSON_ASHOST => 'sap.example.com',
    IConfigTypeA::JSON_SYSNR  => '999',
    IConfiguration::JSON_CLIENT => '001',
    IConfiguration::JSON_USER   => 'username',
    IConfiguration::JSON_PASSWD => 'password'
]);

Mandatory configuration parameters

Type A:

Type B:

Common:

Optional configuration parameters

Type A:

Type B:

Common:


Go back to usage overview | Continue with configuring the SAP remote function call