Answers.Body: <p>In a standalone test script, I could call the SMF createPost() function by first requiring some SMF files:</p>
  
  <pre><code>require_once('/path/to/forum/index.php'); 
  require_once('/path/to/forum/Sources/Subs-Post.php');
  createPost($msgOptions, $topicOptions, $posterOptions);
  </code></pre>
  
  <p>But I ran into php variable scope problems when trying to include index.php from my hooks plugin in the middle of Civi's chain of processing code.  index.php include()'s Settings.php which defines important variables, but they don't wind up in the global scope, so when index.php then calls various other functions they don't have access to the variables.</p>
  
  <p>I hacked around this based on something I found somewhere.  I used this code within the postProcess hook to manually put all the variables defined in Settings.php into the global scope:</p>
  
  <pre><code>$__defined_vars = array_keys(get_defined_vars());
  require_once('/path/to/forum/Settings.php');
  $__newly_defined_vars = array_diff(
    array_keys(get_defined_vars()),
    $__defined_vars,
    array('__defined_vars')
  );
  foreach ($__newly_defined_vars as $var)
    $GLOBALS[$var] = &amp;$$var;
  $GLOBALS['smcFunc'] = array(); //also need to define this in GLOBALS
  require_once('/path/to/forum/index.php'); //this and next line needed to call SMF createPost function
  require_once('/path/to/forum/Sources/Subs-Post.php');
  createPost($msgOptions, $topicOptions, $posterOptions);
  </code></pre>
  
  <p>I welcome other input on better ways to do this...but it works!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-15T17:17:28.597Z
Answers.LastActivityDate: 2015-06-15T17:17:28.597Z
Answers.UserDisplayName: Norris
Body: <p>I want to write a postProcess hook to post submitted form info to a Simple Machines Forum.  How do I call SMF functions from Civi?</p>
  
Tags: <hooks><integration>
Title: How do I call an SMF function from CiviCRM?
ThreadText: # How do I call an SMF function from CiviCRM?
  
  (Asked by: username_0 on 2015-06-15)
  
  I want to write a postProcess hook to post submitted form info to a Simple Machines Forum. How do I call SMF functions from Civi?
  
  **Tags:** hooks, integration
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-06-15. Score: 2
  
  In a standalone test script, I could call the SMF createPost() function by first requiring some SMF files:
  require_once('/path/to/forum/index.php'); 
  require_once('/path/to/forum/Sources/Subs-Post.php');
  createPost($msgOptions, $topicOptions, $posterOptions);
  
  But I ran into php variable scope problems when trying to include index.php from my hooks plugin in the middle of Civi's chain of processing code.  index.php include()'s Settings.php which defines important variables, but they don't wind up in the global scope, so when index.php then calls various other functions they don't have access to the variables.
  I hacked around this based on something I found somewhere.  I used this code within the postProcess hook to manually put all the variables defined in Settings.php into the global scope:
  $__defined_vars = array_keys(get_defined_vars());
  require_once('/path/to/forum/Settings.php');
  $__newly_defined_vars = array_diff(
    array_keys(get_defined_vars()),
    $__defined_vars,
    array('__defined_vars')
  );
  foreach ($__newly_defined_vars as $var)
    $GLOBALS[$var] = &$$var;
  $GLOBALS['smcFunc'] = array(); //also need to define this in GLOBALS
  require_once('/path/to/forum/index.php'); //this and next line needed to call SMF createPost function
  require_once('/path/to/forum/Sources/Subs-Post.php');
  createPost($msgOptions, $topicOptions, $posterOptions);
  
  I welcome other input on better ways to do this...but it works!
  
  
  ---
  
  