如何纠正JPATH_BASE从非根目录运行PHP脚本


How to correct JPATH_BASE to run PHP script from a directory not root

我使用AcyMailing API脚本,它工作得很好,但只在Joomla根下-如http://example.com/api.php

但是如果我把相同的脚本放在我自己的目录下,在相同的Joomla,让我们说/scripts/api.php -我有Apache错误的第12行

PHP Fatal error:  require_once(): Failed opening required '/var/www/html/joomla/scripts/includes/defines.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/joomla/scripts/api.php on line 12

代码中令人困惑的部分是

define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
if (file_exists(dirname(__FILE__) . '/defines.php')) {
 include_once dirname(__FILE__) . '/defines.php';
}
if (!defined('_JDEFINES')) {
 define('JPATH_BASE', dirname(__FILE__));
 require_once JPATH_BASE.'/includes/defines.php';
}

其中第12行是

 require_once JPATH_BASE.'/includes/defines.php';

如何修改路径,使api.php不仅可以从Joomla根目录运行,还可以从/scripts/api.php运行?

提前感谢你的任何想法。

EDITED -这里是完整的脚本,推荐使用JPATH_SITE -但再次出现错误

PHP Fatal error:  require_once(): Failed opening required 'JPATH_SITE/includes/defines.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/joomla/scripts/singlemail.php on line 12

下面的完整脚本可能遗漏了什么?

<?php
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
if (file_exists(dirname(__FILE__) . '/defines.php')) {
 include_once dirname(__FILE__) . '/defines.php';
}
if (!defined('_JDEFINES')) {
 define('JPATH_BASE', dirname(__FILE__));
 require_once JPATH_SITE.'/includes/defines.php';
}
require_once JPATH_SITE.'/includes/framework.php';
$app = JFactory::getApplication('site');

if(!include_once(rtrim(JPATH_ADMINISTRATOR,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_acymailing'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'helper.php')){
 echo 'This code can not work without the AcyMailing Component';
 return false;
 }
$mailer = acymailing_get('helper.mailer');
$mailer->report = true;
$mailer->trackEmail = true;
$mailer->autoAddUser = false;
$mailer->sendOne(11,'test@example.com');
?>

使用JPATH_SITE

如:http://example.com/mysite/api.php

这里的mysite是子文件夹这里你安装joomla的时候,JPATH_BASE有一些问题。

如:1. http://example.com/mysite/api.php2. http://example.com/api.php

下面的路径将在所有地方工作,因为JPATH_SITE将找到joomla安装文件夹。

require_once JPATH_SITE。"/包括/defines.php";