Page 1 of 2

webservice implementation using php

PostPosted: Thu Feb 28, 2019 8:20 pm
by Antonio Linares
webservice.php
Code: Select all  Expand view
<?php      
    header("Content-Type:application/json");

    $response[ 'status' ] = "ready";
    $response[ 'about' ] = "FiveTech Software S.L. webservice";
    $response[ 'method' ] = $_SERVER[ 'REQUEST_METHOD' ];

    echo json_encode( $response );
?>


To test it:
http://www.fivetechsoft.com/webservice.php

Re: webservice implementation using php

PostPosted: Fri Mar 01, 2019 7:44 am
by Carles
Hi,

Basic code

Code: Select all  Expand view
#include 'fivewin.ch'

#define URL_ENDPOINT "http://www.fivetechsoft.com/webservice.php"

FUNCTION Main()

    LOCAL oURL          := TUrl():New( URL_ENDPOINT )
    LOCAL oClient       := TIpClientHttp():New( oUrl )
    LOCAL hRequest      := {=>}
    LOCAl cJson

    IF oClient:Open()   
   
        cJson := oClient:ReadAll()

        hb_jsonDecode( cJson, @hRequest )
           
        xBrowse( hRequest , 'Test Webservice' )    
       
        oClient:Close()
       
    ENDIF
   
RETU NIL
 


Image

Re: webservice implementation using php

PostPosted: Fri Mar 01, 2019 8:11 am
by Antonio Linares
Thank you Carles! :-)

Now lets enhance our webservice to inspect any provided parameters:

webservice.php
Code: Select all  Expand view
<?php      
    header("Content-Type:application/json");

    $response[ 'status' ] = "ready";
    $response[ 'about' ] = "FiveTech Software S.L. webservice";
    $response[ 'method' ] = $_SERVER[ 'REQUEST_METHOD' ];
    $response[ 'params' ] = $_GET;

    echo json_encode( $response );
?>


We can try it this way:
http://www.fivetechsoft.com/webservice.php?tablename=users

And we get this:
{"status":"ready","about":"FiveTech Software S.L. webservice","method":"GET","params":{"tablename":"users"}}

Re: webservice implementation using php

PostPosted: Fri Mar 01, 2019 8:19 am
by Antonio Linares
Lets asume that we want to open a remote database table and retrieve a SQL query:

http://www.fivetechsoft.com/webservice.php?database=test&tablename=users&username=fivetech&password=1234&sql=select%20*

We get this:
{"status":"ready","about":"FiveTech Software S.L. webservice","method":"GET","params":{"database":"test","tablename":"users","username":"fivetech","password":"1234","sql":"select *"}}

Re: webservice implementation using php

PostPosted: Fri Mar 01, 2019 8:21 am
by hmpaquito
Buenos días Antonio,

Interesante tema.
¿ Conectaremos con nuestro sistema fwh, al menos a nivel de datos (dbf) ?
¿ Podremos ejecutar un .exe fwh que genere un json ?

Salu2

Re: webservice implementation using php

PostPosted: Fri Mar 01, 2019 8:26 am
by Antonio Linares
Paco,

Carles has already published the basic code to use it :-)

viewtopic.php?p=219690#p219690

Now we are going to enhance our webservice to offer databases tables management

Re: webservice implementation using php

PostPosted: Fri Mar 01, 2019 8:42 am
by Antonio Linares
A very basic example to allow SQL queries from our webservice:

webservice.php
Code: Select all  Expand view
<?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
    {
       $response[ 'result' ] = mysqli_query( $conn, $sql );
       mysqli_close( $conn );
    }  

    echo json_encode( $response );
?>

Re: webservice implementation using php

PostPosted: Fri Mar 01, 2019 9:59 am
by Carles
Hi,

Basic code (with params)

Code: Select all  Expand view
#include 'fivewin.ch'

#define URL_ENDPOINT "http://www.fivetechsoft.com/webservice.php"

FUNCTION Main()

    LOCAL oURL          := TUrl():New( URL_ENDPOINT )
    LOCAL oClient       := TIpClientHttp():New( oUrl )
    LOCAL hRequest      := {=>}
    LOCAl hParam        := {=>}
    LOCAl cJson

    IF oClient:Open()   
   
        hParam[ 'database' ] = 'test'
        hParam[ 'tablename'] = 'users'
        hParam[ 'username' ] = 'fivetech'
        hParam[ 'password' ] = '1234'
        hParam[ 'sql'      ] = 'select *'
       
        oClient:oURL:addGetForm( hParam )   
   
        cJson := oClient:ReadAll( hParam )

        hb_jsonDecode( cJson, @hRequest )
           
        xBrowse( hRequest, 'Test WebService' )     
   
        oClient:Close()
       
    ENDIF
   
RETURN NIL



Image

Re: webservice implementation using php

PostPosted: Fri Mar 01, 2019 10:21 am
by AngelSalom
:shock: :shock:

Re: webservice implementation using php

PostPosted: Fri Mar 01, 2019 1:54 pm
by cnavarro
Antonio Linares wrote:Lets asume that we want to open a remote database table and retrieve a SQL query:

http://www.fivetechsoft.com/webservice.php?database=test&tablename=users&username=fivetech&password=1234&sql=select%20*

We get this:
{"status":"ready","about":"FiveTech Software S.L. webservice","method":"GET","params":{"database":"test","tablename":"users","username":"fivetech","password":"1234","sql":"select *"}}


Also

Code: Select all  Expand view

//----------------------------------------------------------------------------//
// Acceso básico a un webservice
//----------------------------------------------------------------------------//

#include "Fivewin.ch"

Static cAuthoriza   := ""
Static cBaseUrl     := "http://www.fivetechsoft.com/"
Static cUrlAccess   := "webservice.php"
Static cResponse    := ""
Static aHeaderResp  := {}

//----------------------------------------------------------------------------//

Function Main()

   local hResponse
   local cTextSend
   local cUrl

   cTextSend  := "?database=test&tablename=users&username=fivetech&password=1234&sql=select *"
   cUrl       := cBaseUrl + cUrlAccess + cTextSend
   cResponse  := HRequest( cUrl, , "application/json", , "GET" )
   hb_jsonDecode( cResponse, @hResponse )
   XBrowse( hResponse )
   XBrowse( aHeaderResp )

Return nil

//----------------------------------------------------------------------------//

Function HRequest( cUrl, cParams, cContentType, cAuthorization, cType )

   local oOle
   local cRet    := ""

   DEFAULT cUrl           := cBaseUrl + cUrlAccess
   DEFAULT cParams        := ""
   DEFAULT cContentType   := "application/x-www-form-urlencoded"
   DEFAULT cAuthorization := cAuthoriza
   DEFAULT cType          := "POST"

   // Metodo A
   TRY
      oOle := CreateObject( "MSXML2.ServerXMLHTTP.6.0" )
   CATCH
      oOle := CreateObject( "Microsoft.XMLHTTP" )
   END

   if !Empty( oOle )
      CursorWait()
      oOle:Open( cType, cUrl, .F. )
      oOle:SetRequestHeader( "Content-Type", cContentType )
      if !Empty( cAuthorization )
         oOle:SetRequestHeader( "Authorization", cAuthorization )
      endif
      TRY
         oOle:Send( cParams )
         oOle:WaitForResponse( 1000 )
         cRet    := oOle:ResponseText
         aHeaderResp := Hb_ATokens( oOle:getAllResponseHeaders(), CRLF )
      CATCH
      END
      CursorArrow()
   endif

Return cRet

//----------------------------------------------------------------------------//

 

Re: webservice implementation using php

PostPosted: Fri Mar 01, 2019 5:03 pm
by Antonio Linares
Enhanced webservice:

webservice.php
Code: Select all  Expand view
<?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:
www.fivetechsoft.com/webservice.php?database=fivetech_webservice&username=fivetech_test&password=webservice&sql=SELECT * FROM `users`


Result
Code: Select all  Expand view
{
    "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  Expand view
#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

Re: webservice implementation using php

PostPosted: Fri Mar 01, 2019 5:50 pm
by cnavarro
Also

Code: Select all  Expand view

//----------------------------------------------------------------------------//

Function Main()

   local hResponse
   local cTextSend
   local cUrl

   cTextSend  := "?database=fivetech_webservice&username=fivetech_test&password=webservice&sql=SELECT * FROM `users`"
   cUrl       := cBaseUrl + cUrlAccess + cTextSend
   cResponse  := HRequest( cUrl, , "application/json", , "GET" )
   hb_jsonDecode( cResponse, @hResponse )
   XBrowse( hResponse[ "result" ] )
   XBrowse( hResponse )
   XBrowse( aHeaderResp )

Return nil

//----------------------------------------------------------------------------//
 


Image

Re: webservice implementation using php

PostPosted: Sat Mar 02, 2019 1:22 am
by cnavarro
With Curl

Code: Select all  Expand view

#ifndef HBCURL_CH_

#define HB_CURLOPT_URL                    2
#define HB_CURLOPT_HEADER                42
#define HB_CURLOPT_HTTPHEADER            23
#define HB_CURLOPT_ACCEPT_ENCODING      102
#define HB_CURLOPT_DL_BUFF_SETUP       1008
#define HB_CURLOPT_ENCODING            HB_CURLOPT_ACCEPT_ENCODING

#endif

//----------------------------------------------------------------------------//

Function WebWithCurl()

   local hResponse
   local cUrl       := cBaseUrl + cUrlAccess
   local cResponse

   cResponse := CurlRequest( cUrl )
   hb_jsonDecode( cResponse, @hResponse )
   XBrowse( hResponse )

Return nil

//----------------------------------------------------------------------------//

Function CurlRequest( cUrl )

   local oCurl
   local cResult
   local cTextSend

   curl_global_init()
   oCurl := curl_easy_init()

   if !empty( oCurl )
      curl_easy_setopt( oCurl, HB_CURLOPT_HEADER, 0 )
      curl_easy_setopt( oCurl, HB_CURLOPT_ENCODING, "UTF-8" )
      curl_easy_setopt( oCurl, HB_CURLOPT_HTTPHEADER, 'Content-Type: application/json' )
      curl_easy_setopt( oCurl, HB_CURLOPT_DL_BUFF_SETUP )
      cTextSend  := "database=" + curl_easy_escape( oCurl, "fivetech_webservice" ) + "&"
      cTextSend  += "username=" + curl_easy_escape( oCurl, "fivetech_test" ) + "&"
      cTextSend  += "password=" + curl_easy_escape( oCurl, "webservice" ) + "&"
      cTextSend  += "sql=" + curl_easy_escape( oCurl, "SELECT * FROM `users`" )
      curl_easy_setopt( oCurl, HB_CURLOPT_URL, cUrl + "?" + cTextSend )
      cResult    := curl_easy_perform( oCurl )
      if Empty( cResult )
         cRet    := curl_easy_dl_buff_get( oCurl )
      else
         cRet    := StrZero( cResult, 5 ) + " " + curl_easy_strerror( cResult )
      endif
      curl_easy_reset( oCurl )
      curl_easy_cleanup( oCurl )
   endif
   curl_global_cleanup()

Return cRet

//----------------------------------------------------------------------------//
 

Re: webservice implementation using php

PostPosted: Thu Mar 28, 2019 8:13 pm
by gabrielgaspar.dev
Hello, good afternoon!

I am new here and i wanted to know what i need to access "https"?
I have the Fivewin 17.07 and xHarbour 1.2.3 and BCC 7

Re: webservice implementation using php

PostPosted: Thu Mar 28, 2019 9:19 pm
by cnavarro
You are welcome
Gabriel, explain better what you need