• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Read from JSON/Cached JSON

#1
Hi,

I want use some data from RESTFul API (JSON) and show in sidebar, so I created a template and include in PAGE_CONTAINER,
and in new template I used <xen:callback> to include and run my php code, in php code I use file_get_contents to read from json.
everything seems to be ok, but I have 2 questions:

1- I want to know my way is true or xenForo has some methods for processing JSON APIs?
2- how can I cache JSON data? because every time the page load, server should connect to JSON url !


Regards
 

Chris D

XenForo developer
Staff member
#2
XenForo has a HTTP client which you could use. It doesn't really specifically add anything to handle JSON data, but it's obviously a more robust way of downloading data from a HTTP source.

You could use the XenForo simple cache:
PHP:
// Set simple cache data:
XenForo_Application::setSimpleCacheData('yourUniqueCacheKey', $yourDataToCache);

// Get simple cache data:
$dataFromCache = XenForo_Application::getSimpleCacheData('yourUniqueCacheKey');

// Permanently delete simple cache data:
XenForo_Application::setSimpleCacheData('yourUniqueCacheKey', false);
 
#3
XenForo has a HTTP client which you could use. It doesn't really specifically add anything to handle JSON data, but it's obviously a more robust way of downloading data from a HTTP source.

You could use the XenForo simple cache:
PHP:
// Set simple cache data:
XenForo_Application::setSimpleCacheData('yourUniqueCacheKey', $yourDataToCache);

// Get simple cache data:
$dataFromCache = XenForo_Application::getSimpleCacheData('yourUniqueCacheKey');

// Permanently delete simple cache data:
XenForo_Application::setSimpleCacheData('yourUniqueCacheKey', false);
Thanks Chris,

Would you please guide me about XenForo HTTP methods too?


Regards
 

Chris D

XenForo developer
Staff member
#4
Sorry, that would help ;)

Look at the XenForo_Helper_Http class and how it is used in various places throughout the XF code.
 
#5
XenForo has a HTTP client which you could use. It doesn't really specifically add anything to handle JSON data, but it's obviously a more robust way of downloading data from a HTTP source.

You could use the XenForo simple cache:
PHP:
// Set simple cache data:
XenForo_Application::setSimpleCacheData('yourUniqueCacheKey', $yourDataToCache);

// Get simple cache data:
$dataFromCache = XenForo_Application::getSimpleCacheData('yourUniqueCacheKey');

// Permanently delete simple cache data:
XenForo_Application::setSimpleCacheData('yourUniqueCacheKey', false);
Is there any way to set expire time ?


Regards
 

Chris D

XenForo developer
Staff member
#8
You just need to handle refreshing that cache however you want. So maybe storing the last update time, and doing a refresh once that time plus 2-3 hours has past.
 

Xon

Well-known member
#9
Keep in mind that data in Simple Cache is loaded on every request. This includes ajax calls, css, deferred task etc.

You want to keep the contents small, or store it in a custom Data Registry key which you explicitly load when you need it.