json Web Service in module

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
bbonora
Forum Members
Forum Members
Posts: 48
Joined: Mon Nov 06, 2006 6:10 am

json Web Service in module

Post 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
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: json Web Service in module

Post 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.
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.
bbonora
Forum Members
Forum Members
Posts: 48
Joined: Mon Nov 06, 2006 6:10 am

Re: json Web Service in module

Post 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.
Post Reply

Return to “Developers Discussion”