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 ?
-
☆ 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
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;
}
}
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?