<?php
/** CONFIG:START **/
$config["host"] = "localhost" ; //host
$config["user"] = "modx265" ; //Username SQL
$config["pass"] = "modx265" ; //Password SQL
$config["dbase"] = "modx265" ; //Database
$config["utf8"] = true ; //turkish charset set false
$config["timezone"] = "Asia/Jakarta" ; // check this site: http://php.net/manual/en/timezones.php
$config["abs_url_images"] = "http://domain.com/apps/mymodxblog//media/image/" ; //Absolute Images URL
$config["abs_url_videos"] = "http://domain.com/apps/mymodxblog//media/media/" ; //Absolute Videos URL
$config["abs_url_audios"] = "http://domain.com/apps/mymodxblog//media/media/" ; //Absolute Audio URL
$config["abs_url_files"] = "http://domain.com/apps/mymodxblog//media/file/" ; //Absolute Files URL
$config["image_allowed"][] = array("mimetype"=>"image/jpeg","ext"=>"jpg") ; //whitelist image
$config["image_allowed"][] = array("mimetype"=>"image/jpg","ext"=>"jpg") ;
$config["image_allowed"][] = array("mimetype"=>"image/png","ext"=>"png") ;
$config["file_allowed"][] = array("mimetype"=>"text/plain","ext"=>"txt") ;
$config["file_allowed"][] = array("mimetype"=>"","ext"=>"tmp") ;
/** CONFIG:END **/
date_default_timezone_set($config['timezone']);
if(isset($_SERVER["HTTP_X_AUTHORIZATION"])){
list($_SERVER["PHP_AUTH_USER"],$_SERVER["PHP_AUTH_PW"]) = explode(":" , base64_decode(substr($_SERVER["HTTP_X_AUTHORIZATION"],6)));
}
$rest_api=array("data"=>array("status"=>404,"title"=>"Not found"),"title"=>"Error","message"=>"Routes not found");
/** connect to mysql **/
$mysql = new mysqli($config["host"], $config["user"], $config["pass"], $config["dbase"]);
if (mysqli_connect_errno()){
die(mysqli_connect_error());
}
if(!isset($_GET["json"])){
$_GET["json"]= "route";
}
if((!isset($_GET["form"])) && ($_GET["json"] == "submit")) {
$_GET["json"]= "route";
}
if($config["utf8"]==true){
$mysql->set_charset("utf8");
}
$get_dir = explode("/", $_SERVER["PHP_SELF"]);
unset($get_dir[count($get_dir)-1]);
$main_url = "http://" . $_SERVER["HTTP_HOST"] . implode("/",$get_dir)."/";
switch($_GET["json"]){
// TODO: -+- Listing : site_content
case "site_content":
$rest_api=array();
$where = $_where = null;
// TODO: -+----+- statement where
if(isset($_GET["id"])){
if($_GET["id"]!="-1"){
$_where[] = "`id` LIKE '%".$mysql->escape_string($_GET["id"])."%'";
}
}
if(isset($_GET["pagetitle"])){
if($_GET["pagetitle"]!="-1"){
$_where[] = "`pagetitle` LIKE '%".$mysql->escape_string($_GET["pagetitle"])."%'";
}
}
if(isset($_GET["blogit_post_main_image"])){
if($_GET["blogit_post_main_image"]!="-1"){
$_where[] = "`blogit_post_main_image` LIKE '%".$mysql->escape_string($_GET["blogit_post_main_image"])."%'";
}
}
if(isset($_GET["introtext"])){
if($_GET["introtext"]!="-1"){
$_where[] = "`introtext` LIKE '%".$mysql->escape_string($_GET["introtext"])."%'";
}
}
if(isset($_GET["content"])){
if($_GET["content"]!="-1"){
$_where[] = "`content` LIKE '%".$mysql->escape_string($_GET["content"])."%'";
}
}
if(is_array($_where)){
$where = " WHERE " . implode(" AND ",$_where);
}
// TODO: -+----+- orderby
$order_by = "`id`";
$sort_by = "DESC";
if(!isset($_GET["order"])){
$_GET["order"] = "`id`";
}
// TODO: -+----+- sort asc/desc
if(!isset($_GET["sort"])){
$_GET["sort"] = "desc";
}
if($_GET["sort"]=="asc"){
$sort_by = "ASC";
}else{
$sort_by = "DESC";
}
if($_GET["order"]=="id"){
$order_by = "`id`";
}
if($_GET["order"]=="pagetitle"){
$order_by = "`pagetitle`";
}
if($_GET["order"]=="blogit_post_main_image"){
$order_by = "`blogit_post_main_image`";
}
if($_GET["order"]=="introtext"){
$order_by = "`introtext`";
}
if($_GET["order"]=="content"){
$order_by = "`content`";
}
if($_GET["order"]=="random"){
$order_by = "RAND()";
}
$limit = 100;
if(isset($_GET["limit"])){
$limit = (int)$_GET["limit"] ;
}
// TODO: -+----+- SQL Query
$sql = "SELECT * FROM `modx_site_content` ".$where."ORDER BY ".$order_by." ".$sort_by." LIMIT 0, ".$limit." " ;
if($result = $mysql->query($sql)){
$z=0;
while ($data = $result->fetch_array()){
if(isset($data['id'])){$rest_api[$z]['id'] = $data['id'];}; # id
if(isset($data['pagetitle'])){$rest_api[$z]['pagetitle'] = $data['pagetitle'];}; # heading-1
/** images**/
$abs_url_images = $config['abs_url_images'].'/';
$abs_url_videos = $config['abs_url_videos'].'/';
$abs_url_audios = $config['abs_url_audios'].'/';
if(!isset($data['blogit_post_main_image'])){$data['blogit_post_main_image']='undefined';}; # images
if((substr($data['blogit_post_main_image'], 0, 7)=='http://')||(substr($data['blogit_post_main_image'], 0, 8)=='https://')){
$abs_url_images = $abs_url_videos = $abs_url_audios = '';
}
if(substr($data['blogit_post_main_image'], 0, 5)=='data:'){
$abs_url_images = $abs_url_videos = $abs_url_audios = '';
}
if($data['blogit_post_main_image'] != ''){
$rest_api[$z]['blogit_post_main_image'] = $abs_url_images . $data['blogit_post_main_image']; # images
}else{
$rest_api[$z]['blogit_post_main_image'] = ''; # images
}
if(isset($data['introtext'])){$rest_api[$z]['introtext'] = $data['introtext'];}; # text
if(isset($data['content'])){$rest_api[$z]['content'] = $data['content'];}; # to_trusted
$z++;
}
$result->close();
if(isset($_GET["id"])){
if(isset($rest_api[0])){
$rest_api = $rest_api[0];
}
}
}
break;
// TODO: -+- route
case "route": $rest_api=array();
$rest_api["site"]["name"] = "MyModxBlog" ;
$rest_api["site"]["description"] = "My Modx blog Json" ;
$rest_api["site"]["imabuilder"] = "rev18.07.25" ;
$rest_api["routes"][0]["namespace"] = "site_content";
$rest_api["routes"][0]["tb_version"] = "Upd.1807311159";
$rest_api["routes"][0]["methods"][] = "GET";
$rest_api["routes"][0]["args"]["id"] = array("required"=>"false","description"=>"Selecting `site_content` based `id`");
$rest_api["routes"][0]["args"]["pagetitle"] = array("required"=>"false","description"=>"Selecting `site_content` based `pagetitle`");
$rest_api["routes"][0]["args"]["blogit_post_main_image"] = array("required"=>"false","description"=>"Selecting `site_content` based `blogit_post_main_image`");
$rest_api["routes"][0]["args"]["introtext"] = array("required"=>"false","description"=>"Selecting `site_content` based `introtext`");
$rest_api["routes"][0]["args"]["content"] = array("required"=>"false","description"=>"Selecting `site_content` based `content`");
$rest_api["routes"][0]["args"]["order"] = array("required"=>"false","description"=>"order by `random`, `id`, `pagetitle`, `blogit_post_main_image`, `introtext`, `content`");
$rest_api["routes"][0]["args"]["sort"] = array("required"=>"false","description"=>"sort by `asc` or `desc`");
$rest_api["routes"][0]["args"]["limit"] = array("required"=>"false","description"=> "limit the items that appear","type"=>"number");
$rest_api["routes"][0]["_links"]["self"] = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"]."?json=site_content";
break;
// TODO: -+- submit
case "submit":
$rest_api=array();
$rest_api["methods"][0] = "POST";
$rest_api["methods"][1] = "GET";
break;
}
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET,PUT,POST,DELETE,PATCH,OPTIONS');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization,X-Authorization');
if (!isset($_GET["callback"])){
header('Content-type: application/json');
if(defined("JSON_UNESCAPED_UNICODE")){
echo json_encode($rest_api,JSON_UNESCAPED_UNICODE);
}else{
echo json_encode($rest_api);
}
}else{
if(defined("JSON_UNESCAPED_UNICODE")){
echo strip_tags($_GET["callback"]) ."(". json_encode($rest_api,JSON_UNESCAPED_UNICODE). ");" ;
}else{
echo strip_tags($_GET["callback"]) ."(". json_encode($rest_api) . ");" ;
}
}
Generated this via a rest api generator script.
I am also using REST API of Modx as Bob sir and I discussed here
https://forums.modx.com/thread/102949/rest-api-in-modx-to-build-an-android-app
Both giving same results. I want to know how do I get tv data like tagger tags or post thumb images so I can use them in app to get featured image in blog list page.
I want it like this -
{
"id": "4",
"pagetitle": "Post 2",
"introtext": "Demo summary of post 2 for the BlogIt extra.",
"featured_media": "https://domain.com/image.jpg",
"tags": "sports, football",
"content": "<p>Demo content of post 2 for the BlogIt extra.</p>"
},