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();
//ConfigCommon methods are inherited by ConfigTypeA
$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 class phpsap\classes\Config\ConfigCommon provides the method jsonDecode($string) for directly decoding a JSON encoded configuration into a configuration class.

<?php
use phpsap\classes\Config\ConfigCommon;
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 ConfigCommon determine the type of configuration
 * (A or B) of your formerly encoded JSON.
 */
$configA = ConfigCommon::jsonDecode($jsonConfig);
/**
 * You can always create an instance of your config
 * using already decoded arrays ...
 */
$anotherConfigA = new ConfigTypeA(json_decode($jsonConfig, true));
// ... or objects.
$stillConfigA = new ConfigTypeA(json_decode($jsonConfig, false));

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.

All possible configuration parameters are constants 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;
/**
 * ConfigCommon constants are inherited by ConfigTypeA.
 */
$config = new ConfigTypeA([
    ConfigTypeA::JSON_ASHOST => 'sap.example.com',
    ConfigTypeA::JSON_SYSNR  => '999',
    ConfigTypeA::JSON_CLIENT => '001',
    ConfigTypeA::JSON_USER   => 'username',
    ConfigTypeA::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