Enhanced webservice:
webservice.php
Code: Select all
<?php
header("Content-Type:application/json");
$response[ 'status' ] = "ready";
$response[ 'about' ] = "FiveTech Software S.L. webservice";
$response[ 'method' ] = $_SERVER[ 'REQUEST_METHOD' ];
$response[ 'params' ] = $_GET;
$server = "localhost";
$database = $response[ 'params' ][ 'database' ];
$user = $response[ 'params' ][ 'username' ];
$password = $response[ 'params' ][ 'password' ];
$sql = $response[ 'params' ][ 'sql' ];
$conn = mysqli_connect( $server, $user, $password, $database );
if( ! $conn )
$response[ 'error' ] = mysqli_connect_error();
else
{
$result = mysqli_query( $conn, $sql );
$response[ 'result' ] = array();
while( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ) )
array_push( $response[ 'result' ], $row );
mysqli_close( $conn );
}
echo json_encode( $response );
?>
To test it:
Result
Code: Select all
{
"status": "ready",
"about": "FiveTech Software S.L. webservice",
"method": "POST",
"params": {
"database": "fivetech_webservice",
"username": "fivetech_test",
"password": "webservice",
"sql": "SELECT * FROM `users`"
},
"result": [
{
"name": "Hello world!",
"surname": ""
},
{
"name": "second row",
"surname": ""
},
{
"name": "third name",
"surname": "third surname"
}
]
}
webservice.prg
Code: Select all
#include 'fivewin.ch'
function Main()
LOCAL oClient := TIpClientHttp():New( "http://www.fivetechsoft.com/webservice.php" )
LOCAL hRequest := {=>}
LOCAl hParams := {=>}
LOCAl cJson
if oClient:Open()
hParams[ "database" ] = "fivetech_webservice"
hParams[ "username" ] = "fivetech_test"
hParams[ "password" ] = "webservice"
hParams[ "sql" ] = "SELECT * FROM `users`"
oClient:oUrl:AddGetForm( hParams )
cJson = oClient:ReadAll()
hb_jsonDecode( cJson, @hRequest )
xBrowse( hRequest, 'Test WebService' )
oClient:Close()
endif
return nil