Hello,
I would like to include a json api within my module and was wondering if anybody has ever done this before. Basically I would like to create an easy way for others to include content from my module on their website using a json feed generated by module.
I was planning on creating a new file within my module that would look something like this - action.jsonfeed.php. This file would perform various queries based on URL parameters and then output the result as a json string. I feel pretty comfortable with this process.
What I'm struggling with is how to create a pretty URL that would call the "action.jsonfeed.php" file directly and be accessible to anyone who wanted to use it. I've thought about creating a page within CMSMS and within that page including a tag like {MyModule action="jsonfeed"}. The page template would be blank and only contain a {content} block but this seems a little messy to me.
Does anyone have any ideas for a better way to achieve this?
Thanks,
Ben
json Web Service in module
-
- Support Guru
- Posts: 8169
- Joined: Tue Oct 19, 2004 6:44 pm
- Location: Fernie British Columbia, Canada
Re: json Web Service in module
You need to 'register a route to the action'.
See the RegisterRoute module method, and for a reference see the way other modules use it.. like News.
When returning json or xml or file content
you will also need to add the showtemplate=false parameter
and clear all buffers before you output the data,
and rather than return you will need to exit.
See the RegisterRoute module method, and for a reference see the way other modules use it.. like News.
When returning json or xml or file content
you will also need to add the showtemplate=false parameter
and clear all buffers before you output the data,
and rather than return you will need to exit.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Re: json Web Service in module
I've made a little progress. In the MyModuleName.module.php file I've added the following lines:
In the config.php file I've turned on internal pretty URL's. I can now navigate to http://mydomain.com/feed/.
For testing purposes in the action.feed.php file I've got something like this:
Unfortunately nothing is displayed when I navigate to this link. Is there a way to turn off the template without using the "showtemplate=false" parameter? Also, can you show me an example of the "exit" and "clear buffer" items you're referring to.
Code: Select all
function SetParameters()
{
$gCms = cmsms();
$contentops = $gCms->GetContentOperations();
$returnid = $contentops->GetDefaultContent();
$parms = array('action'=>'feed','returnid'=>$returnid);
$route = new CmsRoute('feed',$this->GetName(),$parms,TRUE);
cms_route_manager::register($route);
}
For testing purposes in the action.feed.php file I've got something like this:
Code: Select all
if (!isset($gCms)) exit;
<?php
echo "It works!";
?>