PHP code example of avadaneidanut / sapphp

1. Go to this page and download the library: Download avadaneidanut/sapphp 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/ */

    

avadaneidanut / sapphp example snippets




use SapPhp\Connection;
use SapPhp\Exceptions\BoxNotFoundException;

try {
	$connection = new Connection(
		'box', // SAP Box Name
		'user', // SAP Username
		'passwd', // SAP Password
		'500' // SAP Client Code
	);
} catch(sapnwrfcConnectionException $ex) {
	// Do something if login failed.
} catch(BoxNotFoundException $ex) {
	// Do something if box doesn't exist.
}



// ... connection

// Instantiate new Function Module interface.
$function = $connection->fm(
	'BAPI_USER_GET_DETAIL', // RFC Enable FM
	true // Parse result (trim all strings and decode GUIDs)
);

// Get function description.
print_r($function->description());

// Add import parameter.
// Will trigger an \SapPhp\Exceptions\ParamNotFoundException if param is not found in function description.
$function->param('USERNAME', 'USER');

// Perform function call and retrieve result.
$result = $function->invoke();



// ... connection

$function = $connection->fm('RFC_READ_TABLE');

$function->param('QUERY_TABLE', 'USR01')
	->param('OPTIONS', [
		['TEXT' => 'BNAME = \'USER\' OR BNAME = \'USER2\' OR BNAME LIKE \'USER5*\'']
	])
	->param('ROWCOUNT', 5)
	->param('DELIMITER', '~')
;

$result = $function->invoke();



// ... connection

// fm method will check if RfcReadTable is an FunctionModule interface Class, if so will return a new instance.
$function = $connection->fm('RfcReadTable'); 

// Let's do the same thing as before.
$result = $function->table('usr01') // set the query table
	->where('bname', ['USER', 'USER5']) // add multiple where clause (simulating where in )
	->orWhere('bname', 'LIKE', 'USER5*') // add custom comparation operator
	->limit(5) // limit the result to 5 rows
	->get() // perform function call, parse the result and return a \Illuminate\Support\Collection object.
;

print_r($result->toArray());



$query->where('column', 'value') // Add WHERE clause
	->andWhere('column2,' 'value2') // AND logical operarator
	->orWhere('column3', '<>', 'value3') // OR logical operator
	->orWhere(function ($query) { // WHERE group
		$query->where('column11', 'value11')
			->andWhere('column22', 'value22');
	})
	->orWhere('column5', '<>', [1, 2, 3, 4]); // Simulate WHERE IN clause




// ... connection & function

$rfcReadTable->table(['table1', 'table2', 'table3'], function($agregatter) {
	$aggregate->table('table1')
		->with('table2')
		->on('column')
		->as('aggregated_table');
	$aggregate->table('aggregated_table', 'table3')
		->on('column2');
})->where('column', 'value');

[
	[
		"MANDT" => "500",
		"BNAME" => "USER2",
		"STCOD" => "",
		"SPLD" => "",
		"SPLG" => "",
		"SPDB" => "H",
		"SPDA" => "K",
		"DATFM" => "1",
		"DCPFM" => "",
		"HDEST" => "",
		"HMAND" => "",
		"HNAME" => "",
		"MENON" => "",
		"MENUE" => "",
		"STRTT" => "",
		"LANGU" => "",
		"CATTKENNZ" => "",
		"TIMEFM" => "0",
	],
	[
		"MANDT" => "500",
		"BNAME" => "USER5",
		"STCOD" => "",
		"SPLD" => "",
		"SPLG" => "",
		"SPDB" => "H",
		"SPDA" => "K",
		"DATFM" => "1",
		"DCPFM" => "",
		"HDEST" => "",
		"HMAND" => "",
		"HNAME" => "",
		"MENON" => "",
		"MENUE" => "",
		"STRTT" => "",
		"LANGU" => "",
		"CATTKENNZ" => "",
		"TIMEFM" => "0",
	],
	[
		"MANDT" => "500",
		"BNAME" => "USER55",
		"STCOD" => "",
		"SPLD" => "",
		"SPLG" => "",
		"SPDB" => "H",
		"SPDA" => "K",
		"DATFM" => "1",
		"DCPFM" => "",
		"HDEST" => "",
		"HMAND" => "",
		"HNAME" => "",
		"MENON" => "",
		"MENUE" => "",
		"STRTT" => "",
		"LANGU" => "",
		"CATTKENNZ" => "",
		"TIMEFM" => "0",
	],
]