JSON endpoints with ezjscore
This page is for application creators whose pages need data from PHP without a page reload: autocomplete, live
counters, small saves. ezjscore calls registered PHP functions over HTTP and answers in JSON
(ezjscore, Backend ezjscore services).
Contents
- How a call works
- Registering a server function
- The class
- Calling it from JavaScript
- Security
- ezjscore, expservices or REST?
How a call works
<root>/ezjscore/call/<name>::<function>[::<arg>...]?ContentType=jsonThe router (ezjscServerRouter) refuses any name without a block in ezjscore.ini, calls <class>::<function>( array $args ) and wraps the return value: {"error_text": "", "content": <your value>}. A test function ships:
<root>/ezjscore/call/ezjsc::time.
Registering a server function
# extension/expdemo/settings/ezjscore.ini.append.php
[ezjscServer_expdemo]
Class=expDemoServerFunctions
*/ ?>Optional keys (see the comments of [ezjscServer] in extension/ezjscore/settings/ezjscore.ini): File,
TemplateFunction, Functions[] (policy functions of [ezjscServer] FunctionList[] the user needs).
The class
class expDemoServerFunctions
{
public static function count( array $args )
{
$user = eZUser::currentUser();
$access = $user->hasAccessTo( 'expdemo', 'read' );
if ( $access['accessWord'] === 'no' )
throw new Exception( 'Access denied' );
$userID = isset( $args[0] ) ? (int)$args[0] : 0;
return array( 'count' => eZPersistentObject::count( expDemoItem::definition(), array( 'user_id' => $userID ) ) );
}
}ezjscore/call does not check a policy by itself: every function checks for itself.
Calling it from JavaScript
// extension/expdemo/design/standard/javascript/expdemo.js
fetch( root + 'ezjscore/call/expdemo::count::' + encodeURIComponent( userId ) + '?ContentType=json',
{ credentials: 'same-origin', headers: { 'Accept': 'application/json' } } )
.then( function ( r ) { return r.json(); } )
.then( function ( data ) { if ( !data.error_text ) show( data.content.count ); } );root comes from a JSON data block in the pagelayout, not an inline script
(Content Security Policy for your code). Writes are POSTs and carry the form token.
Security
- Check permissions in every function; validate and cast every argument.
- Return data, not HTML; if you must return HTML, escape it.
- Writes only by POST, with the form token.
- Do not expose more than the page needs.
ezjscore, expservices or REST?
| For | Use |
|---|---|
| your own pages' JavaScript | ezjscore server functions |
| admin features from scripts, apps or other front ends | expservices, with discovery, one envelope and API tokens (Remote services) |
| content for other programs, OAuth for third parties | REST (REST API) |
Related pages
- Remote services with expservices
- Stylesheets and scripts for your site
- Extension points: server-side ajax function
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.