Thanks for the suggestion - I will need to read up on REST Endpoints as I’ve never used them.
in your case, really just needs to be a very simple snippet, which gets the latest resource - object and returns the needed fields as json.
Put this snippet into the content of a new resource with a blank template and maybe contenttype JSON
on the other site call the url of that resource with for example file_get_contents, decode that json and pass it to getChunk
Bruno has a great answer.
Maybe take a look at: https://docs.modx.com/current/en/extending-modx/modx-class/loading-externally
Thanks - I will have a read and see if I can get my head round it!
Thanks again for the advice. I will have to see if I can find more info on this somewhere. I don’t understand this enough. I’ve read the ‘Loading the modx object’ article (thanks) but I don’t understand it enough to know where I go from there. I’m a visual designer and build out modx sites but don’t really stray from the built in templating etc.
As I said, you don’t need that. You just need a snippet, which outputs a JSON with the needed data. That’s all
Ah right ok. Sorry, I thought that was all part of one solution using the api etc.
Ok, this sounds more manageable. Will have a play and see if I can get this working. Thanks.
Presumably I could just use getResources then?
you could use pdoResources with &return=json
[[!pdoResources? &return=`json` &parents=`62`]]
Thanks Bruno.
So - I have it partially working with help from you and searching the forums.
In Site A, I have a PdoResources on a resource with no template like so:
[[!pdoResources?
&parents=3
&limit=1
&includeTVs=PDF
&tpl=latestLissueJSONTpl
]]
Then on Site B, I have a snippet on a page which ulls in the data that I need - but I’m not sure how to pass this to getChunk in order to format it.
<?php
$string=file_get_contents('https://www.pathToJsonResource.json');
$json_a=json_decode($string,true);
// associative array mode
echo $json_a[IssueNumber];
echo $json_a[image];
So it echos out the Issue Number and image URl which is great - but not sure how to pass this to a chunk so that I can format it.
I’m almost there!
Thanks for your help so far!
Andy
Although I just read you shouldn’t use echo in a snippet?
<?php
$output = '';
foreach ($json_a as $image) {
$output .= $modx->getChunk('latestLissueJSONTpl',array(
'image' => $image,
));
}
echo $oputput;
?>
*Untested
** Make sure you make the chunk on site B
Wow - thanks!! I’m trying it right now.
I assume I’m merging these togehre into one snippet, but not sure what to keep of mine?
<?php
$string=file_get_contents('https://www.pathToJsonResource.json');
$properties=json_decode($string,true);
if (is_array($properties)){
return $modx->getChunk('yourChunk',$properties);
}
return 'something went wrong';
I think, you don’t need a tpl on your pdoResources - call, when using
&return=`json`
do you?
I just tried without a tpl and it outputs a lot of data rather than just the two fields I require) - is that a performance hit?
I tried your snippet and it works wih the TPL - but not when i use &return: json - Th JSON is formatted differently.
This works great thank you.
But for some reason it’s trying to pull in two resources - the HTML output is:
<p>This is issue: 487</p>
<img src="https://www.domain.co.uk/image.jpg"/>
<p>This is issue: </p>
<img src=""/>
Not sure where the second instance is coming from?
you can use &select to limit the returned fields
what does the fetched json looks like? And your chunk?
maybe, you still have a foreach loop in your snippet, which you don’t need, if you fetch only one resource.