We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5274
    • 177 Posts
    I found the code below here: http://modxcms.com/forums/index.php/topic,24701.0.html

    Quote from: sirlancelot at Apr 14, 2008, 01:58 PM

    <?php
    // Set this to your MODx Base Path
    $base_path = '/var/www/modx/';
    
    define('MODX_API_MODE', true);
    include($base_path.'index.php');
    $modx->db->connect();
    $modx->getSettings();
    

    This solution is for standalone pages/requests wanting to access MODx resources (Ajax Calls, External CMSs, etc.).

    The code above normally works without issue in my usual procedural programming scripts. I’m trying to adopt OOP practices and can’t get this to work in a function/class. I’ve used functions for other purposes a lot but I’m new to writing my own class. Variable scope still causes me some issues but I seem to be making progress. smiley

    Can somebody help me incorporate the MODX_API_MODE external script access into a class? Here are me test scripts:

    modx.php
    <?php
    
    define('MODX_API_MODE', true); if (defined('MODX_API_MODE')) { echo 'modx.php | MODX_API_MODE | TRUE <br />' . "\n"; }
    
    include 'modx.class.php';
    
    $test = new MODx;
    
    $test -> printMODx();
    
    echo 'modx.php | test <br />' . "\n";
    echo print_r($test) . '<br />' . "\n";
    
    
    ?>
    


    modx.class.php
    <?php
    
    class MODx {
    
      var $modx;
    
      function printMODx() {
    
        echo 'modx.class.php | MODX_API_MODE | '; if (defined('MODX_API_MODE')) { echo 'TRUE <br />' . "\n"; }
        echo 'modx.class.php | modx <br />' . "\n";
        echo print_r($this -> modx) . '<br />' . "\n";
    
      } // END function printMODx
    
      function getMODx() {
    
        include '/home/xxx/www/index.php';
        $this -> modx -> db -> connect();
        $this -> modx -> getSettings();
    
      } // END function getMODx
    
    } // END class MODx
    
    ?>
    


    The simplistic output with a bunch of echos for debug.
    modx.php | MODX_API_MODE | TRUE
    modx.class.php | MODX_API_MODE | TRUE
    modx.class.php | modx
    1
    modx.php | test
    MODx Object ( [modx] => ) 1
    


    No errors in the Apache logs. Is this even doable? I’m really trying to get some "class" in my coding but this has me stumped!
      • 5274
      • 177 Posts
      An update as I continue to modify my code and bulldoze my way through this challenge...

      I’ve seen this before but don’t have a resolution. Now the following error with the associated in my Apache error logs entries.

      Could not load DBAPI class.

      A search of the MODx files tells me that the error was generated by "manager/includes/document.parser.class.inc.php". Just to reiterate, my code works fine if I don’t try to stick it in a function or class.

      [Sat May 10 15:02:38 2008] [error] [client xxx.xxx.xxx.xxx] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/xxx/www/bkupmodx/modx.php:6) in /home/xxx/www/index.php on line 59
      [Sat May 10 15:02:38 2008] [error] [client xxx.xxx.xxx.xxx] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/xxx/www/bkupmodx/modx.php:6) in /home/xxx/www/index.php on line 60
      [Sat May 10 15:02:38 2008] [error] [client xxx.xxx.xxx.xxx] PHP Warning:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by (output started at /home/xxx/www/bkupmodx/modx.php:6) in /home/xxx/www/manager/includes/config.inc.php on line 59
      [Sat May 10 15:02:38 2008] [error] [client xxx.xxx.xxx.xxx] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/xxx/www/bkupmodx/modx.php:6) in /home/xxx/www/manager/includes/config.inc.php on line 73
      [Sat May 10 15:02:38 2008] [error] [client xxx.xxx.xxx.xxx] PHP Warning:  DocumentParser::include_once(/home/xxx/www/manager/includes/extenders/dbapi..class.inc.php) [<a href='documentparser.include-once'>documentparser.include-once</a>]: failed to open stream: No such file or directory in /home/xxx/www/manager/includes/document.parser.class.inc.php on line 39
      [Sat May 10 15:02:38 2008] [error] [client xxx.xxx.xxx.xxx] PHP Warning:  DocumentParser::include_once() [<a href='function.include'>function.include</a>]: Failed opening '/home/xxx/www/manager/includes/extenders/dbapi..class.inc.php' for inclusion (include_path='.:/usr/lib/php') in /home/xxx/www/manager/includes/document.parser.class.inc.php on line 39
      
      
        • 10487 MODX Staff
        • 1,535 Posts
        Well, I think I understand what you’re trying to do ...

        What I do is call the $modx object as a reference to whatever object I need to use it in (using the object constructor), so something like:
        <?php
        class myObject {
           var $modx; 
        
           function myObject(&$modx) {
               $this->modx = $modx;
           }
        }
        

        And, then in your script, you’d have:
        <?php
        // Set this to your MODx Base Path
        $base_path = '/var/www/modx/';
        
        define('MODX_API_MODE', true);
        include_once($base_path.'index.php');
        $modx->db->connect();
        $modx->getSettings();
        
        // create myObject class
        include_once($base_path.'<path_to_object>/myObject.class.php');
        $myObject = new myObject($modx);
        

        Obviously, you’d need to adjust the paths etc. for your external class accordingly. But, then you could access any of the methods in your external class by using $this->modx, for example $this->modx->config[’base_path’]

        Hope that helps,
        Garry
          Garry Nutting
          Senior Developer
          MODX, LLC

          Email: [email protected]
          Twitter: @garryn
          Web: modx.com
          • 5274
          • 177 Posts
          Quote from: garryn at May 10, 2008, 07:07 PM

          Hope that helps,
          Garry


          That is a big help Gary! This enables me to pass the values of the $modx array to a class for processing. How about pushing the following code into the class also?

          include_once($base_path.'index.php');
          $modx->db->connect();
          $modx->getSettings();
          


          That’s where I was getting the most errors. Maybe it can’t be done?

          Thanks!
          Chuck
            • 5274
            • 177 Posts
            I really appreciate being able to have external PHP scripts interact with MODx using the MODX_API_MODE constant! Thank you to those who forged the way and made this feature available in the framework.

            I’ve found that any HTML output including a PHP "echo" in the running script prior to the index.php include command will generate the errors associated with "PHP Warning: Cannot modify header information" and "PHP Warning: session_start()" in the Apache error log. That accounts for 4 of the 6 log entries on my earlier post.

            I’m only starting to "know what I don’t know" about PHP scripting. I hope this might help others that run into similar MODx/PHP learning experiences.