Modx api how to use runSnippet?

For the third time, please check your PHP error log to find the specific error.

Trying to figure it out by guessing the problem is a waste of everyone’s time.

1 Like

What they said, but I’d also suggest a test above the offending line:

if (! $modx instanceof modX) {
     echo "MODX not instantiated";

Markh, please tell how or where to check php errors? By the way, Modx’s log has no errors at all.

Ok, I’ll try it tomorrow and let you know.

As halftrainedharry’s link says, API mode is listed as deprecated.

See this for an alternative approach.

That completely depends on your server configuration and is independent of MODX entirely.

Common locations are within a server control panel, /usr/local/apache/logs/error_log, var/log/httpd/error_log, /var/log/apache2/error.log, as error_log files in the directory the error was triggered in, or elsewhere. On MODX Cloud it’s in your home directory > log.

If you check the phpinfo() output (which MODX has a utility for, in manage > reports > system info > phpinfo()) find the error_log directive. That normally points to the location for the error log. However if that’s empty, it may not write logs anywhere and you’ll need to edit the php.ini or ask you host.

Finally if there’s no way to track it down, adding these two lines in your php file may allow the error message to be shown on screen instead:


Again this is not related to MODX but basic debugging of any php application. Google information specific to how your server is setup if you still can’t find it with this.

The easiest way to create an ajax - endpoint is to create a resource with an empty template and call your snippet there.

1 Like
if (!defined('MODX_CORE_PATH')) {
define('MODX_CORE_PATH', $_SERVER["DOCUMENT_ROOT"].'/core/');
if (!defined('MODX_CONFIG_KEY')) {
define('MODX_CONFIG_KEY', 'config');
require_once( MODX_CORE_PATH . 'model/modx/modx.class.php');
$modx = new modX();

if (! $modx instanceof modX) {
echo json_encode(['error' => "MODX not instantiated"], JSON_UNESCAPED_UNICODE);
echo json_encode(['error' => "MODX instantiated"], JSON_UNESCAPED_UNICODE);

it prints MODX instantiated

php error is

PHP Fatal error: Uncaught Error: Call to a member function runSnippet() on null in /home/…/public_html/theme/RedirectModxApiAjax/RedirectModxApiAjax.php:225\nStack trace:\n#0 /home/…public_html/theme/RedirectModxApiAjax/RedirectModxApiAjax.php(248): tryUpload(Array)\n#1 {main}\n thrown in /home/…/public_html/theme/RedirectModxApiAjax/RedirectModxApiAjax.php on line 225, referer

tryUpload() is a function where I’m trying to runSnippet. runSnippet is line 225

you want to show us the whole thing including your tryUpload function?

This means, that $modx is null. If you instantiated $modx in a different function then probably the scope of this variable is wrong. Maybe you can pass $modx as a parameter to the function tryUpload().

or use global $modx; at the beginning of your function

<input type="file" name="ava[]" id="ava" multiple="" accept="image/jpeg,image/JPEG,image/jpg,image/JPG,image/png,image/PNG,image/bmp,image/BMP,image/heic,image/HEIC">

function tryUpload($file){
$uploaddir = $_SERVER['DOCUMENT_ROOT'].'/user_upload/';
if(!is_dir( $uploaddir)) mkdir($uploaddir, 0777);

echo json_encode(['error' => 'U should upload files'], JSON_UNESCAPED_UNICODE);
$files = array();
foreach($_FILES['ava'] as $k => $l) {
foreach($l as $i => $v) {
$files[$i][$k] = $v;
$_FILES['ava'] = $files;

$total_files = count($files);
$maxTotalFiles = 3;
$maxFileLimit = 4194304;
if($total_files > $maxTotalFiles){
echo json_encode(['error' => 'More then 3'], JSON_UNESCAPED_UNICODE);
for($key = 0; $key < $total_files; $key++) {
echo json_encode(['error' => 'One of files has no name'], JSON_UNESCAPED_UNICODE);
if($file['size'][$key] > $maxFileLimit){
echo json_encode(['error' => 'One of file is more then 4 mb'], JSON_UNESCAPED_UNICODE);
$ext = pathinfo($file['name'][$key], PATHINFO_EXTENSION);
$types = array('jpg', 'jpeg', 'heic', 'png', 'bmp', 'JPG', 'JPEG', 'HEIC', 'PNG', 'BMP');
if(!in_array($ext, $types)){
echo json_encode(['error' => 'U can upload jpg, png, heic, bmp files'], JSON_UNESCAPED_UNICODE);

$fileName = date('YmdHis').'_'.translit($file['name'][$key]);

if(move_uploaded_file($file['tmp_name'][$key], $uploaddir.$fileName)){

global $ava;
global $ava2;
global $ava3;
global $avaLittle;
global $ava2Little;
global $ava3Little;

if ($key === 0) {
$ava = $avaLittle = $_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER["SERVER_NAME"].'/user_upload/'.$fileName;
// $avaLittle = $uploaddir.$fileName;

$options = "w=100&h=100&zc=1&far=C&q=95";
$avaLittle = $modx->runSnippet('phpthumbof', array('input' => $avaLittle, 'options' => $options));

echo json_encode(['error' => $avaLittle], JSON_UNESCAPED_UNICODE);

//    if ($key === 1) {
  //  $ava2 = $ava2Little = $_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER["SERVER_NAME"].'/user_upload/'.$fileName;
//    }
  //  if ($key === 2) {
   // $ava3 = $ava3Little = $_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER["SERVER_NAME"].'/user_upload/'.$fileName;
//    }

  //  } 
else {
    echo json_encode(['error' => "Please upload one file at least"], JSON_UNESCAPED_UNICODE);
    //call the function
global $modx;

The code you posted misses all the important stuff.

  • Where do you instantiate $modx?
  • Where do you call tryUpload()?

All the minutiae of the function tryUpload() don’t matter that much, when you try to determine why $modx is null.

Will jsFieddle be ok for present the whole code?

Maybe just post an overview of the structure of your program here. All the functions and function calls and the parts where you instantiate or use $modx.

function tryUpload($file){
	$modx->runSnippet('phpthumbof', array('input' => $avaLittle, 'options' => $options));

function another_function(){

function initialize_modx(){
	$modx = new modX();

Now at the beginning of tryUpload function I use global $modx; Thank you Bruno17. I get the same error. I’ve changed phpthumbof to units to check if I get the same error (units - is a component for Russian language) and there was no error. Does it mean that something wrong with phpthumbof?

After submitted the form I checked the directory assets/components/phpthumbof/cache/ and I found the image I have upload. So phpthumbof works fine. Then what’s the problem?

As halftrainedharry said, it really looks like you either are not creating (instantiating) the $modx object properly, or the $modx variable is not available where you’re trying to use it.

As a general practice, I always instantiate modx once using the code here, check it with instanceof, then pass it as an argument to any function that needs it.


Hello guys.
Thanks everyone very much. Thanks for your advices. Thanks to you I solved the problem. Everything was fine with API. The problem was that phpthumbof didn’t like the link of the file. No matter absolute link or local link. I don’t know why and I don’t even want to know.

I installed phpthumbsup, didn’t change the code and the snippet worked just fine.

If your problem is same, try to install phpthumbsup