|
| 1 | +## Usage: establish a connection |
| 2 | + |
| 3 | +A new connection object is created using a previously created configuration. |
| 4 | + |
| 5 | +Upon creation of a connection object, the configuration is read from the |
| 6 | + configuration object. In case a mandatory configuration parameter is missing, |
| 7 | + an exception will be thrown. |
| 8 | + |
| 9 | +Creating a connection object doesn't mean an actual connection is established. |
| 10 | + You can determine the actual connection status using `isConnected()` and you |
| 11 | + can use `connect()` to manually establish a connection. |
| 12 | + |
| 13 | +```php |
| 14 | +<?php |
| 15 | +use phpsap\saprfc\SapRfcConfigA; |
| 16 | +use phpsap\saprfc\SapRfcConnection; |
| 17 | + |
| 18 | +$config = new SapRfcConfigA([ |
| 19 | + 'ashost' => 'sap.example.com', |
| 20 | + 'sysnr' => '001', |
| 21 | + 'client' => '002', |
| 22 | + 'user' => 'username', |
| 23 | + 'passwd' => 'password' |
| 24 | +]); |
| 25 | + |
| 26 | +try { |
| 27 | + $connection = new SapRfcConnection($config); |
| 28 | +} catch (\phpsap\interfaces\exceptions\IIncompleteConfigException $exception) { |
| 29 | + printf( |
| 30 | + 'Your SAP configuration is missing mandatory parameter(s). Message: %s', |
| 31 | + $exception->getMessage() |
| 32 | + ); |
| 33 | + die(); |
| 34 | +} |
| 35 | + |
| 36 | +//returns boolean false |
| 37 | +$connection->isConnected(); |
| 38 | + |
| 39 | +//establish an actual connection to the SAP system |
| 40 | +try { |
| 41 | + $connection->connect(); |
| 42 | +} catch (\phpsap\interfaces\exceptions\IConnectionFailedException $exception) { |
| 43 | + printf( |
| 44 | + 'Establishing a connection failed. Message: %s', |
| 45 | + $exception->getMessage() |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +//returns boolean true |
| 50 | +$connection->isConnected(); |
| 51 | + |
| 52 | +//closes the connection |
| 53 | +$connection->close(); |
| 54 | + |
| 55 | +//returns boolean false |
| 56 | +$connection->isConnected(); |
| 57 | +``` |
| 58 | + |
| 59 | +### Ping a connection |
| 60 | + |
| 61 | +You can determine whether a remote function call is possible by pinging a |
| 62 | + connection using `ping()`. |
| 63 | + |
| 64 | +Calling `ping()` will automatically call `connect()` and can therefore throw an |
| 65 | + `IConnectionFailedException`. |
| 66 | + |
| 67 | +```php |
| 68 | +<?php |
| 69 | +use phpsap\saprfc\SapRfcConfigA; |
| 70 | +use phpsap\saprfc\SapRfcConnection; |
| 71 | + |
| 72 | +$config = new SapRfcConfigA([ |
| 73 | + 'ashost' => 'sap.example.com', |
| 74 | + 'sysnr' => '001', |
| 75 | + 'client' => '002', |
| 76 | + 'user' => 'username', |
| 77 | + 'passwd' => 'password' |
| 78 | +]); |
| 79 | + |
| 80 | +//leave out try+catch for simplification |
| 81 | +$connection = new SapRfcConnection($config); |
| 82 | + |
| 83 | +try { |
| 84 | + $pingResult = $connection->ping(); |
| 85 | +} catch (\phpsap\interfaces\exceptions\IConnectionFailedException $exception) { |
| 86 | + printf( |
| 87 | + 'Establishing a connection failed. Message: %s', |
| 88 | + $exception->getMessage() |
| 89 | + ); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### Prepare a remote function call |
| 94 | + |
| 95 | +In order to call a SAP remote function you need to prepare the function call |
| 96 | + using `prepareFunction()`. |
| 97 | + |
| 98 | +Calling `prepareFunction()` will automatically call `connect()` and can |
| 99 | + therefore throw an `IConnectionFailedException`. |
| 100 | + |
| 101 | +In case the requested function does not exist, an `IUnknownFunctionException` |
| 102 | + is thrown. |
| 103 | + |
| 104 | +```php |
| 105 | +<?php |
| 106 | +use phpsap\saprfc\SapRfcConfigA; |
| 107 | +use phpsap\saprfc\SapRfcConnection; |
| 108 | + |
| 109 | +$config = new SapRfcConfigA([ |
| 110 | + 'ashost' => 'sap.example.com', |
| 111 | + 'sysnr' => '001', |
| 112 | + 'client' => '002', |
| 113 | + 'user' => 'username', |
| 114 | + 'passwd' => 'password' |
| 115 | +]); |
| 116 | + |
| 117 | +//leave out try+catch for simplification |
| 118 | +$connection = new SapRfcConnection($config); |
| 119 | + |
| 120 | +try { |
| 121 | + $function = $connection->prepareFunction('HELLO_WORLD'); |
| 122 | +} catch (\phpsap\interfaces\exceptions\IConnectionFailedException $exception) { |
| 123 | + printf( |
| 124 | + 'Establishing a connection failed. Message: %s', |
| 125 | + $exception->getMessage() |
| 126 | + ); |
| 127 | +} catch (\phpsap\interfaces\exceptions\IUnknownFunctionException $exception) { |
| 128 | + printf( |
| 129 | + 'The requested remote function does not exist. Message: %s', |
| 130 | + $exception->getMessage() |
| 131 | + ); |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +### The connection ID |
| 136 | + |
| 137 | +A connection object contains a connection ID, which is determined from its |
| 138 | + configuration. That means, that the same configuration will result in the same |
| 139 | + connection ID. |
| 140 | + |
| 141 | +Using this you can compare two connection objects in order to find out, whether |
| 142 | + they use the same configuration. However if you call `connect()` on both |
| 143 | + objects, two separate actual connections to the SAP system will be |
| 144 | + established. |
| 145 | + |
| 146 | +```php |
| 147 | +<?php |
| 148 | +use phpsap\saprfc\SapRfcConfigA; |
| 149 | +use phpsap\saprfc\SapRfcConnection; |
| 150 | + |
| 151 | +$config = new SapRfcConfigA([ |
| 152 | + 'ashost' => 'sap.example.com', |
| 153 | + 'sysnr' => '001', |
| 154 | + 'client' => '002', |
| 155 | + 'user' => 'username', |
| 156 | + 'passwd' => 'password' |
| 157 | +]); |
| 158 | + |
| 159 | +//leave out try+catch for simplification |
| 160 | +$connection1 = new SapRfcConnection($config); |
| 161 | +$connection2 = new SapRfcConnection($config); |
| 162 | + |
| 163 | +//$same will be boolean true |
| 164 | +$same = ($connection1->getId() === $connection2->getId()); |
| 165 | +``` |
| 166 | + |
| 167 | +--- |
| 168 | + |
| 169 | +[Go back to usage overview](usage) |
| 170 | + | [Go back to configuring a connection](saprfc-config) |
| 171 | + | [Continue with invoking a function call](saprfc-connection) |
0 commit comments