XF 2.0 How to access XenForo options via regular PHP

BlueOrange

Member
EDIT: I've managed to solve this myself, by looking around the forums and seeing what is done for people creating threads and users via "external" scripts. My solution is:

PHP:
function GetAPIKey() {
    $dir = $_SERVER["DOCUMENT_ROOT"];
    require("$dir/src/XF.php");

    XF::start($dir);

    $options = XF::options();
    return "$options->BridgeAPIKey";
}


I've been developing a API of sorts and within this API I would like to get the options from a custom addon. How would I do so?
I've tried the following, but nothing is returned when GetAPIKey is called:

PHP:
public static function GetAPIKey() {
    $options = \XF::options();
    return $options["BridgeAPIKey"];
}

public static function GetAPIKey() {
    $options = \XF::options();
    return $options->BridgeAPIKey;
}

Am I missing something? The option was created in the addon options in debug mode too.

Thanks
 
Last edited:
You might consider separating out some of the code. It's good to use the SOLID principle and have one purpose for each method. Also, you'll want a catch for an error if the directory is wrong. The code below doesn't return an error message but it might be a good idea.

Without spending too much time, this is similar to your code but sets a method for the connection as well as a method for getting the Bridge API Key.

PHP:
class AClassNameGoesHere {

   protected $options;

   public function construct() {

   }

   public function GetXF() {

      // What happens when XF is in a subdirectory off the root?
      $fileDir = $_SERVER["DOCUMENT_ROOT"];

      if ( ! file_exists( $fileDir . '/src/XF.php'  ) ) {
         return false;
      }

      require( $fileDir . '/src/XF.php' );
      \XF::start($fileDir);

      $app = \XF::setupApp('XF\Pub\App');
      $app->start();

      return '';
   }

   public function GetBridgeAPIKey(){

      if ( ! class_exists( '\XF' ) ) {
         $this->GetXF();
      }
      $this->options = \XF::options();
      return "$this->options->BridgeAPIKey";
   }
}
 
Back
Top Bottom