Page 1 of 1

json Web Service in module

Posted: Tue Jul 26, 2011 4:43 pm
by bbonora
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

Re: json Web Service in module

Posted: Tue Jul 26, 2011 6:31 pm
by calguy1000
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.

Re: json Web Service in module

Posted: Tue Jul 26, 2011 6:41 pm
by bbonora
I've made a little progress. In the MyModuleName.module.php file I've added the following lines:

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);
  }
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:

Code: Select all

if (!isset($gCms)) exit;
<?php
  echo "It works!";
?>
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.