public function callFunctionTest() { $rfc = $this->open(); $fce = saprfc_function_discover($rfc, 'ZBAPI_RECEIVING'); if (!$fce) { echo 'Discovering interface of function module ZBAPI_RECEIVING failed'; exit; } saprfc_import($fce, RSINPUT, array('ZEX_VBELN' => '1500000000', 'KUNNR' => 'ANGELITO', 'MATNR' => 'ANGELITO0002', 'LFIMG' => '5.000', 'CHARG' => '1427279260', 'WERKS' => 'BBL2', 'LFART' => 'ZEL', 'LGORT' => 'B201', 'XABLN' => 'ZXY12345', 'WADAT' => '04/25/2015', 'WDATU' => '04/25/2015', 'HSDAT' => '04/03/2014', 'VFDAT' => '04/05/2015', 'CRATES_IND' => '', 'EXIDV' => '', 'VHILM' => '36', 'VHILM2' => '36', 'REMARKS' => 'TEST', 'LAST_ITEM_IND' => ' ')); saprfc_import($fce, 'PRINTER', 'ZWI6'); saprfc_table_init($fce, 'ET_PALLETS'); saprfc_table_init($fce, 'ET_PALLETS_W_TO'); $sn = saprfc_export($fce, 'VBELN'); $ol = saprfc_export($fce, 'OBJECT_LOCKED'); $nc = saprfc_export($fce, 'NOT_COMPATIBLE'); $vc = saprfc_export($fce, 'VOLUME_CAP_ERROR'); $oe = saprfc_export($fce, 'OTHER_ERROR'); if (saprfc_call_and_receive($fce) == SAPRFC_OK) { echo '<h2>Pulled Data</h2>'; echo 'SAP #:' . $sn . '<br />'; echo 'Object Locked:' . $ol . '<br />'; echo 'Not Compatible:' . $nc . '<br />'; echo 'Volume Cap Error:' . $vc . '<br />'; echo 'Other Error:' . $oe . '<br />'; } $data_et_pallets = saprfc_table_rows($fce, 'ET_PALLETS'); $data_et_pallets_w_to = saprfc_table_rows($fce, 'ET_PALLETS_W_TO'); $this->close($fce, $rfc); }
public function connect($func, $CDSA) { $conn = inf()->sap->conn; $conn["USER"] = $_SESSION["SAP_USERNAME"]; $conn["PASSWD"] = $_SESSION["SAP_PASSWORD"]; $this->user = $conn["USER"]; $this->conn = saprfc_open($conn); if (!$this->conn) { throw new Exception("SAPRFC CONNECTION FAILED"); } $this->func = saprfc_function_discover($this->conn, $func); if (!$this->func) { throw new Exception(sprintf("DISCOVERING INTERFACE OF FUNCTION %s MODULE FAILED", $func)); } $this->conf = inf()->sap->conf; $rs = DB::lookup("vfs")->executeQuery("SELECT * FROM sap_conf WHERE AREA_SUB='%s'", $CDSA); if ($rs->next()) { $this->conf["BUS_AREA"] = $rs->get("BUS_AREA"); $this->VR = $rs->get("VENDOR"); $this->VC = $rs->get("VENDOR_CO"); $this->CC = $rs->get("COST_CENTER"); $this->PC = $rs->get("PROFIT_CENTER"); } else { throw new Exception(sprintf("LOADING SAP CONFIG FOR FUNCTION %s MODULE FAILED", $func)); } }
/** * Discover interface of function module * * @param [rfc] RFC handle * @param [name] Name of function module * * @return SAPRFC_OK on success, SAPRFC_ERROR on failure */ function Discover($rfc, $name) { if ($this->fce) { return $this->SetStatus(SAPRFC_ERROR, "SAPFunction::Discover: Function is " . "already defined."); } $this->fce = @saprfc_function_discover($rfc, strtoupper($name)); if ($this->fce == false) { return $this->SetStatus(SAPRFC_ERROR, "SAPFunction::Discover: " . "Failed for function module " . $name . "\n" . "with error " . @saprfc_error()); } $this->rfc = $rfc; $this->name = @saprfc_function_name($this->fce); $this->exception = ""; $this->server = false; $this->bapi = substr($this->name, 0, 4) == "BAPI"; $this->def = $this->GetDefinition(); $this->InitVars(); return $this->SetStatus(SAPRFC_OK, ""); }
/** * sapConn * * @param string $conn - Connection string "Test","Production","Production_wm" * @param string $module - Sap Module to connect ... * @return saprfc_function reference * @access private */ public function sapConn($conn, $module) { if (isset($this->sapMod[$module]) and $this->sapMod[$module]) { return $this->sapMod[$module]; } if (!$this->sapRfc && !$this->saplogin($conn)) { trigger_error("Sap connection failed!", E_USER_ERROR); $this->errorDetails[] = array('Result' => '0', 'Message' => 'Ligação ao servido Sap falhou!'); return false; } $sapModule = $module; $this->sapMod[$module] = saprfc_function_discover($this->sapRfc, $sapModule); if (!$this->sapMod[$module]) { trigger_error('SAP module function not found', E_USER_ERROR); $this->errorDetails[] = array('Result' => '0', 'Message' => 'Módulo de função ' . $sapModule . ' não encontrado'); return false; } return $this->sapMod[$module]; }
/** * Check if RFC connection is valid * * @return SAPRFC_OK on success, <> SAPRFC_OK on failure */ function IsConnectionLive() { if ($this->rfc == false) { return $this->SetStatus(SAPRFC_ERROR, "SAPConnection::IsConnectionLive: No valid RFC handle"); } $fce = @saprfc_function_discover($this->rfc, "RFC_PING"); if ($fce == false) { return $this->SetStatus(SAPRFC_ERROR, @saprfc_error()); } $rc = @saprfc_call_and_receive($fce); @saprfc_function_free($fce); if ($rc == SAPRFC_OK) { return $this->SetStatus(SAPRFC_OK, ""); } else { return $this->SetStatus(SAPRFC_ERROR, @saprfc_error()); } }
function bapiCommit($bapiName) { global $rfc, $fce; $fce = saprfc_function_discover($rfc, $bapiName); if (!$fce) { echo "Discovering interface of function module failed"; exit; } $rfc_rc = saprfc_call_and_receive($fce); // var_dump($rfc_rc); if ($rfc_rc != SAPRFC_OK) { if ($rfc == SAPRFC_EXCEPTION) { echo "Exception raised: " . saprfc_exception($fce); } else { echo saprfc_error($fce); } exit; } }
/** * Get debug information * @param $name * @return mixed */ public function debug($name) { ob_start(); $fce = saprfc_function_discover($this->connection, $name); saprfc_function_debug_info($fce); return ob_get_clean(); }
function callFunction($func_name="",$parameters) { /* typical call: $result=$saprfc->call_function("RFC_SYSTEM_INFO", array( array("EXPORT","SYSTEM","MBS") array("IMPORT","CODEPAGE") array("IMPORT","DBNAME") array("TABLE","APPLLIST",array()) ) ); */ // Initialize Variables $result_data=array(); $this->call_function_result=false; // Check SAPRFC-Installation if (! extension_loaded ("saprfc")) { return $this->setStatus(SAPRFC_ERROR,"saprfc::callFunction()\n SAPRFC-Extension.dll not loaded."); } // Validate given data if (empty($func_name)) { return $this->setStatus(SAPRFC_ERROR,"saprfc::callFunction():\n No Function-Name given"); } // Move Parameters to local Arrays $func_params_import=array(); $func_params_tables=array(); $func_params_export=array(); foreach ($parameters as $key => $param) { $type=$param[0]; $name=$param[1]; $value=isset($param[2])?$param[2]:""; switch ($type) { case "IMPORT": $func_params_import[$name]=$value; break; case "EXPORT": $func_params_export[$name]=""; break; case "TABLE": $func_params_tables[$name]=$value; if (!is_array($value)) { return $this->setStatus(SAPRFC_ERROR,"saprfc::callFunction()\n Wrong Parameter-Value for Table-Parameter ".$name.". We expected an Array."); } break; default: return $this->setStatus(SAPRFC_ERROR,"saprfc::callFunction()\n Wrong Parameter-Type '".$type."'. Must be IMPORT, EXPORT or TABLE."); } } // Do Login (only done in method login(), if necessary) if ($this->login()==SAPRFC_ERROR) { return $this->getStatus(); } // Discover Function in SAP $this->func_id=@saprfc_function_discover($this->rfc_conn,$func_name); if (!$this->func_id) { return $this->setStatus(SAPRFC_ERROR,"saprfc::callFunction()\n Function module '".$func_name."' seems not to exist. function saprfc_function_discover() failed."); } // Set Import-Parameters foreach ($func_params_import as $name => $value) { $rc=@saprfc_import($this->func_id,$name,$value); if (empty($rc)) { return $this->setStatus(SAPRFC_ERROR,"saprfc::callFunction('".$func_name."')\n Import-Parameter=".$name. " could not be set. (Does it exist?)"); } } // Set Table-Parameters (importing-values) foreach ($func_params_tables as $name => $value) { $rc=@saprfc_table_init($this->func_id,$name); if (empty($rc)) { return $this->setStatus(SAPRFC_ERROR,"saprfc::callFunction('".$func_name."')\n Table-Parameter=".$name. " could not be set. (Does it exist?)"); } foreach ($value as $key => $data) { @saprfc_table_append($this->func_id,$name,$data); } } // Execute Function $result = @saprfc_call_and_receive ($this->func_id); if ($result != SAPRFC_OK) { if ($result == SAPRFC_EXCEPTION ) { return $this->setStatus(SAPRFC_ERROR,"saprfc::callFunction('".$func_name."')\n saprfc_call_and_receive(): Exception raised: ".@saprfc_exception($this->func_id)); } else { return $this->setStatus(SAPRFC_ERROR,"saprfc::callFunction('".$func_name."')\n saprfc_call_and_receive(): Call error: ".@saprfc_error($this->func_id)); } } // Get Exporting-Parameters foreach ($func_params_export as $name =>$value) { $rc=@saprfc_export($this->func_id,$name); // if (empty($rc)) { // llaegner removed (Reason: when export returns the -good- value "0", then empty also evaluates to true (thanks Alexander Gouriev) if (!isset($rc)) { return $this->setStatus(SAPRFC_ERROR,"saprfc::callFunction('".$func_name."')\n Export-Parameter=".$name. " could not be set (Does it exist?)".@saprfc_error($this->func_id)); } else { $result_data[$name]=$rc; } } // Get Table-Parameters foreach ($func_params_tables as $name => $content) { // Ausgabe-Tabelle initialisieren $result_data[$name]=array(); $rows=@saprfc_table_rows($this->func_id,$name); for ($i=1; $i<=$rows; $i++) { $result_data[$name][$i]=@saprfc_table_read ($this->func_id,$name,$i); } } // Save function-call result for later analysis $this->call_function_result=$result_data; // Echo Debug-Information, if flagged if ($this->debug) @saprfc_function_debug_info($this->func_id); // Falls es ein BAPI-Aufruf ist, Fehler raussuchen if ($this->check_bapi_errors) { $bapi_return=@saprfc_export($this->func_id,"RETURN"); if (isset($bapi_return) && is_array($bapi_return) && isset($bapi_return["MESSAGE"]) && $bapi_return["NUMBER"] != 0) { // FINISH FUNCTION-CALL $this->setStatus(SAPRFC_APPL_ERROR,$bapi_return); return $result_data; } } // Close Function-Execution and free results removed because it sometimes stops completly executing PHP!! //@saprfc_function_free($this->func_id); // FINISH FUNCTION-CALL $this->setStatus(SAPRFC_OK,"call function '".$func_name."' successfull."); return $result_data; }
"SYSNR"=>"30", // system number "CLIENT"=>"900", // client "USER"=>"rfctest", // user "PASSWD"=>"*****", // password "CODEPAGE"=>"1404"); // codepage // ---------------------------------------------------------------------------- $rfc = saprfc_open ($LOGIN); if (! $rfc ) { echo "RFC connection failed with error:".saprfc_error(); exit; } $fce = saprfc_function_discover($rfc, "RFC_READ_REPORT"); if (! $fce ) { echo "Discovering interface of function module RFC_READ_REPORT failed"; exit; } saprfc_import ($fce,"PROGRAM",$REPORT); saprfc_table_init ($fce,"QTAB"); $rc = saprfc_call_and_receive ($fce); if ($rfc_rc != SAPRFC_OK) { if ($rfc == SAPRFC_EXCEPTION ) echo ("Exception raised: ".saprfc_exception($fce)); else
/** * @param SAP_Module_Abstract $module * * @throws Exception * @return array * @author Manuel Will * @since 2013 */ public function executeSave(SAP_Module_Abstract $module) { $this->storeDebugTraceToStatic(); $moduleName = $module->getModuleName(); $moduleResource = $this->getModuleResource($moduleName); $receivedData = @saprfc_call_and_receive($moduleResource); $this->checkStatusCode($receivedData, $moduleResource); /** * Ein Commit kann trotzdem stattfinden, obwohl ein Resultat zurückkommt. * Ein Rollback braucht es eigentlich nur beim Fehler, der wirklich ein Fehler ist. Der Fehler würde dann aber schon * in $this->checkStatusCode eine Exception werfen. Möglicherweise erübrigt sich ein Rollback eigentlich immer. Hier sollte wohl immer * Array zurückkommen weshalb das nur mit if ($var) geprüft wurde. Bei manchen Bausteinen kommt es wohl vor, dass auch -1 zurück kommen kann. * Das heißt, es kommt ein "Fehlerresult" zurück, aber nicht als Array sondern als "-1". Das ist offensichtlich kein Fehler. */ $mxdResult = saprfc_table_rows($moduleResource, 'RETURN'); if ($mxdResult && $mxdResult != -1) { $moduleResource2 = saprfc_function_discover($this->getConnectionResource(), 'BAPI_TRANSACTION_ROLLBACK'); $errorCode = saprfc_call_and_receive($moduleResource2); $this->checkStatusCode($errorCode, $moduleResource2); } else { $moduleResource2 = saprfc_function_discover($this->getConnectionResource(), 'BAPI_TRANSACTION_COMMIT'); $errorCode = saprfc_call_and_receive($moduleResource2); $this->checkStatusCode($errorCode, $moduleResource2); } $successState = @saprfc_table_read($moduleResource, 'RETURN', 1); // if (!empty($successState['TYPE']) && $successState['TYPE'] == 'E') { throw new SAP_Exception(json_encode($successState)); } $result = array(); $this->appendExporter($result, $moduleResource, $module->getExporter()); // saprfc_function_debug_info($moduleConnection); saprfc_function_free($moduleResource); if (PHP_SAPI == 'cli') { echo print_r($this->debug) . PHP_EOL; } return array('transactionSuccess' => false === $successState, 'export' => $result); // d.h. insert hat funktioniert! }
if (!isset ($action) ) $action = ""; if ($action == "") // if enter from selection page, set last entries cookie { if (! isset ($e) || ! is_array ($e)) $e = array(); $t_e = $e; unset ($e); $e[0] = strtoupper($function); foreach ($t_e as $t_func) { if (strtoupper($function) != $t_func) $e[] = $t_func; } unset ($t_e); $SAPRFC_ENTRIES=serialize($e); setcookie ("SAPRFC_ENTRIES", urlencode($SAPRFC_ENTRIES),time()+3600); } $fce = @saprfc_function_discover ($rfc,$function); // discover interface of function module $function if (! $fce ) { print_error_message ( "Discovering function module error", "Can't discover interface of function module <b>$function</b>. Please check if the module is Remote-enable (transaction SE37, Atrributes).<a href=\"saprfc_test.php?p=select\">Other function module</a>", saprfc_error() ); exit; } $def = @saprfc_function_interface($fce); // retrieve definition of interface in array $def // $def has following format: /* $def = array ( array ("name"=>"PARAMETER", // name of import, export parameter or internal table "type"=>"IMPORT", // type: IMPORT, EXPORT, TABLE "optional"=>0 // 1 - optional parameter "def"=> typedef // type definition (elementary SAP data type or structure)