FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour webservice implementation using php
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
webservice implementation using php
Posted: Thu Feb 28, 2019 08:20 PM
webservice.php
Code (fw): Select all Collapse
<?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
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1283
Joined: Fri Feb 10, 2006 02:34 PM
Re: webservice implementation using php
Posted: Fri Mar 01, 2019 07:44 AM
Hi,

Basic code

Code (php): Select all Collapse
<div class="php" id="{CB}" style="font-family: monospace;">#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

    <a href="http://www.php.net/if">IF</a> oClient:Open()   
    
        cJson := oClient:ReadAll()

        hb_jsonDecode( cJson, @hRequest )
            
        xBrowse( hRequest , 'Test Webservice' )     
        
        oClient:Close()
        
    <a href="http://www.php.net/endif">ENDIF</a>
    
RETU NIL
 </div>


Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
HIX -> https://github.com/carles9000/hix
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: webservice implementation using php
Posted: Fri Mar 01, 2019 08:11 AM
Thank you Carles! :-)

Now lets enhance our webservice to inspect any provided parameters:

webservice.php
Code (fw): Select all Collapse
<?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"}}
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: webservice implementation using php
Posted: Fri Mar 01, 2019 08:19 AM
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 *"}}
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: webservice implementation using php
Posted: Fri Mar 01, 2019 08:21 AM

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

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: webservice implementation using php
Posted: Fri Mar 01, 2019 08:26 AM

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

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: webservice implementation using php
Posted: Fri Mar 01, 2019 08:42 AM
A very basic example to allow SQL queries from our webservice:

webservice.php
Code (fw): Select all Collapse
<?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 );
?>
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1283
Joined: Fri Feb 10, 2006 02:34 PM
Re: webservice implementation using php
Posted: Fri Mar 01, 2019 09:59 AM
Hi,

Basic code (with params)

Code (PHP): Select all Collapse
<div class="php" id="{CB}" style="font-family: monospace;">#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

    <a href="http://www.php.net/if">IF</a> oClient:Open()   
    
        hParam[ 'database' ] = 'test'
        hParam[ 'tablename'] = 'users'
        hParam[ 'username' ] = 'fivetech'
        hParam[ 'password' ] = '1234'
        hParam[ 'sql'      ] = 'select *'
        
        oClient:addGetForm( hParam )   
    
        cJson := oClient:ReadAll( hParam )

        hb_jsonDecode( cJson, @hRequest )
            
        xBrowse( hRequest, 'Test WebService' )      
    
        oClient:Close()
        
    <a href="http://www.php.net/endif">ENDIF</a>
    
<a href="http://www.php.net/return">RETURN</a> NIL</div>



Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
HIX -> https://github.com/carles9000/hix
Posts: 728
Joined: Fri Oct 07, 2005 07:38 AM
Re: webservice implementation using php
Posted: Fri Mar 01, 2019 10:21 AM

:shock::shock:

Angel Salom
Visionwin Software - https://www.visionwin.com
------------------------------------------------------------
fwh 19.05 - harbour 3.2 - bcc 7.4
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: webservice implementation using php
Posted: Fri Mar 01, 2019 01:54 PM
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 (fw): Select all Collapse
//----------------------------------------------------------------------------//
// 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

//----------------------------------------------------------------------------//
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: webservice implementation using php
Posted: Fri Mar 01, 2019 05:03 PM
Enhanced webservice:

webservice.php
Code (fw): Select all Collapse
<?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 (fw): Select all Collapse
{
    "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 (fw): Select all Collapse
#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
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: webservice implementation using php
Posted: Fri Mar 01, 2019 05:50 PM
Also

Code (fw): Select all Collapse
//----------------------------------------------------------------------------//

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

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


Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: webservice implementation using php
Posted: Sat Mar 02, 2019 01:22 AM
With Curl

Code (fw): Select all Collapse
#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

//----------------------------------------------------------------------------//
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 4
Joined: Thu Mar 28, 2019 01:35 PM
Re: webservice implementation using php
Posted: Thu Mar 28, 2019 08:13 PM

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

Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: webservice implementation using php
Posted: Thu Mar 28, 2019 09:19 PM

You are welcome
Gabriel, explain better what you need

Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces