Modx api how to use runSnippet?

Hello everyone.
I’m new in Modx.
I’m trying to launch runSnippet command (phpthumbof), but I’m still keep getting error .
There is html form. Submitting the form ajax sends data to php file.


//  modx api
define('MODX_API_MODE', true);
require $_SERVER['DOCUMENT_ROOT'].'/index.php';


$options = "w=100&h=100&zc=1&far=C&q=95";
$image = $_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER["SERVER_NAME"].'/user_upload/'.$fileName; // it's ok
$userImage = $modx->runSnippet('phpthumbof', array('input' => $image, 'options' => $options)); // if I comment this line I won't get the error

echo json_encode(['error' => '<img src="'.$userImage.'" alt="">'], JSON_UNESCAPED_UNICODE);

The error is {"readyState":4,"responseText":"","status":500,"statusText":"error"}

Maybe you could check the error-log of your server to see what the actual error message is.

The runSnippet-call looks good to me. Perhaps $modx isn’t initialized correctly.
Maybe check this site as the MODX_API_MODE seems to be deprecated anyway.

Unfortunately, same error

Like @halftrainedharry said, check your PHP error log for details of the error. A 500 status can mean anything, and the PHP error log will immediately tell you what it is.

Likely there’s something wrong in your file, or pThumb/phpthumbof expects to have access to something (perhaps a context or resource) which you’re not initializing.

Hello. Like I said if I comment the line $image = $_SERVER....... everything works fine. That’s mean there are no errors in my file. That’s mean the problem is runSnippet line or I didn’t include some important Modx methods || files || processors || etc… in my file.

One interesting thing I’m thinking about the whole day is that I’ve got another html page where I call a snippet - [[!check]]. Inside this snippet I call phpthumbof jus like $avatar = $modx->runSnippet('phpthumbof', array('input' => $var, 'options' => $options)); You know what? It works just fine. $var is Why? Why does 2st snippet work by calling it inside 1st snippet? Can some of you create a file, include Modx API and try to simulate my situation?

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?