Recommendation on creating API Server with Scriptcase

Would like to request some pointers on creating an API server with Scriptcase that can be called from a Wordpress site and returns content?

What is the recommended type of application for the API? What standards are recommended for such a type of Web API development?

[QUOTE=bengrech;41553]Would like to request some pointers on creating an API server with Scriptcase that can be called from a Wordpress site and returns content?

What is the recommended type of application for the API? What standards are recommended for such a type of Web API development?[/QUOTE]

There are two ways to go imho: soap and/or rest interface. For simplicity I would choose a rest interface. You can build those using blank applications, retrieve the url input and respond in xml, json, html etc.

Thanks for the pointer Albert.

If yoy just need an API, do it in plain PHP. SC don’t offers you anything if you will not use the UI.
If you still want to use SC. Blank app is the way to go

Here is an example if that helps

  1. I have created a blank application
  2. created a php function and called this function from OnExecute event

The following is the content of the php function under Programming PHP methods

header(‘Access-Control-Allow-Origin: *’);
header(‘Content-Type: application/json’);

$data_arr = array();
$data_arr[‘data’] = array();

$check_sql = ‘SELECT categoryid, categoryname’
. ’ FROM categories’;

sc_select(rs, $check_sql);

if (false == {rs})
{
echo json_encode(array(‘message’ => ‘Error while accessing database.’));
}
else
{
while(!$rs->EOF)
{
$id = $rs->fields[0];
$categoryname = $rs->fields[1];

$post_item = array(
‘id’ => $id,
‘category’ => $categoryname
);

array_push ($data_arr[‘data’], $post_item);

$rs->MoveNext();
}
$rs->Close();
echo json_encode($data_arr);
}

hi nnara530,
i’d like your code and wanted to automate it from the sql query. i made something like this but not working correctly. it gives all colums as single json object… it generates the json but the format is not correct. can you help?
i want to send the query to the function json_automate and it will return the json format.

$sql_query = “Select name, surname, telephone, mail FROM database”;

function json_automate($sql_query) {

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');

// this gives the column names 
$preg = trim((preg_match('@SELECT(.*?)FROM@', $sql_query, $matches)));
$columns = explode(",",trim($matches[1]));


$data_arr = array();
$data_arr['data'] = array();

    sc_select(rs, $sql_query);
    if (false == {rs})
    {
        echo json_encode(array('message' => 'Error while accessing database.'));
    }
    else
    {
        while(!$rs->EOF)
        {
            for($i=0; $i<count($columns); $i++){
            array_merge($columns[$i],$rs->fields[$i]);    

            $post_item = array(

                $columns[$i] => $rs->fields[$i]

            );

            array_push ($data_arr['data'], $post_item);

            $rs->MoveNext();
            }    
        }
        $rs->Close();
        return json_encode($data_arr);
    }

}

Here Fix the issue. Maybe it will help someone to generate it.
:):rolleyes:

    // this gives the column names
    $preg = trim((preg_match('@select(.*?)from@', $sql_query, $matches)));
    $columns = explode(",",trim($matches[1]));

    $data_arr = array();
    $data_arr['data'] = array();

    sc_select(rs, $sql_query);
    if (false == {rs})
    {
        echo json_encode(array('message' => 'Erro de acceso al a BD.'));
    }
    else
    {
        while(!$rs->EOF)
        {
            for($i=0; $i<count($columns); $i++){
                $post_item[$columns[$i]] = $rs->fields[$i];
            }    

            array_push ($data_arr['data'], $post_item);
            $rs->MoveNext();
        }
    }
    $rs->Close();
    echo json_encode($data_arr);