#1 2018-11-22 19:36:15

triguinhu
Member
From: Brazil
Registered: 2016-07-28
Posts: 27

PHP with sample 16

Hi

Can a Web Client written in PHP be able to use the 16 Server example to run SQL through the SOA Service on the Mormot Server?

If so, where do I find information to create this PHP code?

Offline

#2 2018-11-24 09:42:07

Ehab
Member
Registered: 2016-09-07
Posts: 15

Re: PHP with sample 16

I think you need to write a mORMot client in PHP if the server has authentication.

If there is no authentication then you can use something like the following code.
Note : I don't have PHP now to test this code.

<?php

list($rseOleDB, $rseODBC, $rseOracle, $rseSQlite3, $rseJet, $rseMSSQL) = [0,1,2,3,4,5];

function _callFunction($funcName, $params){
    $mServer = "http://www.your-server.com:888/root";

    $opts = array( 
        'http' => array( 
            'method'=>"POST", 'header'=>"Content-Type: application/json; charset=UTF-8",
            'content'=> json_encode($params)
        ) 
    );       

    $context = stream_context_create($opts); 

    $url = "$mServer/RemoteSQL/$funcName";  //Or $url = "$mServer/RemoteSQL.$funcName";
    return @file_get_contents($url , false, $context); 
}


function connect($engine, $aServerName, $aDatabaseName,  $aUserID, $aPassWord){
   return  _callFunction("Connect", [$engine, $aServerName, $aDatabaseName,  $aUserID, $aPassWord]);
}

function getTables(){
   return  _callFunction("GetTableNames", []);
}

function execute($aSQL, $aExpectResults, $aExpanded){
   return  _callFunction("Execute", [$aSQL, $aExpectResults, $aExpanded]);
}

?>

Offline

#3 2018-11-24 13:20:00

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: PHP with sample 16

What we usually do with PHP or JavaScript clients, is the following:

1. Use interface-based services
2. Always communicate via https (over a nginx proxy with Let's Encrypt certificates)
3. Force full JSON objects for communication (instead of default JSON array which require a contract check) using TSQLRestServer.ServiceDefine(...).ResultAsJSONObjectWithoutResult := true
4. Define JWT authentication, using TSQLRestServer.JWTForUnauthenticatedRequest, and a dedicated interface for retrieving the JWT
5. Use Mustache template for API documentation generation (see the doc)

Then, any PHP or JS programmer would be able to connect using the documentation, via regular JSON + POST over HTTP.

Online

Board footer

Powered by FluxBB