We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48305
    • 58 Posts
    Hi, I want to get results in json format using getresourse, I have checked runSnippet to get array of result in a snippet

    like

    php
    global $modx;
    $list = $modx->runSnippet (
    "getResources" , array(
    "parents"=>125));


    $output = explode(',', $list);
    echo "<pre>".print_r($list )."</pre>
    ";

    but it did't get any result?

    So how i can get array or json using php in modx ?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You just need to use getResources (or pdoResources) with the proper tpl. See https://forums.modx.com/index.php/topic,67146.msg380971.html
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 4172
        • 5,888 Posts
        slightly modified version (lines 24 + 41 added) of pdoResources, name it pdoResources_json should work:

        <?php
        /* @var array $scriptProperties */
        
        if (isset($parents) && $parents === '') {
        	$scriptProperties['parents'] = $modx->resource->id;
        }
        if (!empty($returnIds)) {
        	$scriptProperties['return'] = 'ids';
        }
        
        // Adding extra parameters into special place so we can put them in results
        /** @var modSnippet $snippet */
        $additionalPlaceholders = array();
        if ($snippet = $modx->getObject('modSnippet', array('name' => 'pdoResources'))) {
        	$properties = unserialize($snippet->properties);
        	foreach ($scriptProperties as $k => $v) {
        		if (!isset($properties[$k])) {
        			$additionalPlaceholders[$k] = $v;
        		}
        	}
        }
        $scriptProperties['additionalPlaceholders'] = $additionalPlaceholders;
        
        $scriptProperties['return'] = 'data'; //return results as array
        
        /* @var pdoFetch $pdoFetch */
        $fqn = $modx->getOption('pdoFetch.class', null, 'pdotools.pdofetch', true);
        if ($pdoClass = $modx->loadClass($fqn, '', false, true)) {
        	$pdoFetch = new $pdoClass($modx, $scriptProperties);
        }
        elseif ($pdoClass = $modx->loadClass($fqn, MODX_CORE_PATH . 'components/pdotools/model/', false, true)) {
        	$pdoFetch = new $pdoClass($modx, $scriptProperties);
        }
        else {
        	$modx->log(modX::LOG_LEVEL_ERROR, 'Could not load pdoFetch from "MODX_CORE_PATH/components/pdotools/model/".');
        	return false;
        }
        $pdoFetch->addTime('pdoTools loaded');
        $output = $pdoFetch->run();
        
        $output = $modx->toJson($output); // convert array to JSON
        
        $log = '';
        if ($modx->user->hasSessionContext('mgr') && !empty($showLog)) {
        	$log .= '<pre class="pdoResourcesLog">' . print_r($pdoFetch->getTime(), 1) . '</pre>';
        }
        
        // Return output
        if (!empty($returnIds)) {
        	$modx->setPlaceholder('pdoResources.log', $log);
        	if (!empty($toPlaceholder)) {
        		$modx->setPlaceholder($toPlaceholder, $output);
        	}
        	else {
        		return $output;
        	}
        }
        elseif (!empty($toSeparatePlaceholders)) {
        	$output['log'] = $log;
        	$modx->setPlaceholders($output, $toSeparatePlaceholders);
        }
        else {
        	$output .= $log;
        
        	if (!empty($tplWrapper) && (!empty($wrapIfEmpty) || !empty($output))) {
        		$output = $pdoFetch->getChunk($tplWrapper, array('output' => $output), $pdoFetch->config['fastMode']);
        	}
        
        	if (!empty($toPlaceholder)) {
        		$modx->setPlaceholder($toPlaceholder, $output);
        	}
        	else {
        		return $output;
        	}
        }
        
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 48305
          • 58 Posts
          I need data for development of iPhone app, thats why I want to convert to json, all web articles and other data. Have you any idea for fetching result in iPhone App?
            • 40045
            • 534 Posts
            You could try that with the extra InstantAPI http://modx.com/extras/package/instantapi