Search code examples
phpjoomla

Access articles in Joomla 3.9 from external php file, blank page?


I try to access to article by ID (let say id 179), but in my Joomla 3.9.3 site, it show a blank page and nothing happen. Here is my code (name get.php, it's on same root folder with configuration.php, index.php, robots.txt ...)

<?php
    error_reporting(E_ALL);
    ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    define('_JEXEC', 1);
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
    require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );
    $app = JFactory::getApplication('');
    var_dump($app);
    $app->initialise();
    $db = JFactory::getDBO();// Joomla database object
    $sql = "SELECT fulltext FROM #_content WHERE id = 179";
    $db->setQuery($sql);
    $fullArticle = $db->loadResult();
    if(!strlen(trim($fullArticle))) $fullArticle = "Article is empty ";
    echo $fullArticle;
  ?>

Even nothing show when I var_dump();

What did i do wrong or Joomla 3.9.3 change the way we access?


Solution

  • You can try below code -

    <?php
    error_reporting(E_ALL);
    ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    define('_JEXEC', 1);
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once JPATH_LIBRARIES . '/import.legacy.php';
    require_once JPATH_LIBRARIES . '/cms.php';
    
    $app = JFactory::getApplication('site');
    // var_dump($app);
    // $app->initialise();
    $db = JFactory::getDBO();// Joomla database object
    $sql = "SELECT `fulltext` FROM #__content WHERE id = 179";
    $db->setQuery($sql);
    $fullArticle = $db->loadResult();
    if(!strlen(trim($fullArticle))) $fullArticle = "Article is empty ";
    echo $fullArticle;