webservice implementation using php

User avatar
Antonio Linares
Site Admin
Posts: 42739
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 105 times
Been thanked: 108 times
Contact:

webservice implementation using php

Post by Antonio Linares »

webservice.php

Code: Select all | Expand

<?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
User avatar
Carles
Posts: 1156
Joined: Fri Feb 10, 2006 2:34 pm
Location: Barcelona
Been thanked: 8 times
Contact:

Re: webservice implementation using php

Post by Carles »

Hi,

Basic code

Code: Select all | Expand

#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
Salutacions, saludos, regards

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

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
Skype -> https://join.skype.com/cnzQg3Kr1dnk
User avatar
Antonio Linares
Site Admin
Posts: 42739
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 105 times
Been thanked: 108 times
Contact:

Re: webservice implementation using php

Post by Antonio Linares »

Thank you Carles! :-)

Now lets enhance our webservice to inspect any provided parameters:

webservice.php

Code: Select all | Expand

<?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
User avatar
Antonio Linares
Site Admin
Posts: 42739
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 105 times
Been thanked: 108 times
Contact:

Re: webservice implementation using php

Post 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 *"}}
regards, saludos

Antonio Linares
www.fivetechsoft.com
hmpaquito
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: webservice implementation using php

Post 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
User avatar
Antonio Linares
Site Admin
Posts: 42739
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 105 times
Been thanked: 108 times
Contact:

Re: webservice implementation using php

Post 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42739
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 105 times
Been thanked: 108 times
Contact:

Re: webservice implementation using php

Post by Antonio Linares »

A very basic example to allow SQL queries from our webservice:

webservice.php

Code: Select all | Expand

<?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
User avatar
Carles
Posts: 1156
Joined: Fri Feb 10, 2006 2:34 pm
Location: Barcelona
Been thanked: 8 times
Contact:

Re: webservice implementation using php

Post by Carles »

Hi,

Basic code (with params)

Code: Select all | Expand

#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
Salutacions, saludos, regards

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

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
Skype -> https://join.skype.com/cnzQg3Kr1dnk
User avatar
AngelSalom
Posts: 727
Joined: Fri Oct 07, 2005 7:38 am
Location: Benicarló (Castellón ) - España
Has thanked: 1 time
Contact:

Re: webservice implementation using php

Post by AngelSalom »

:shock: :shock:
Angel Salom
Visionwin Software - https://www.visionwin.com
------------------------------------------------------------
fwh 19.05 - harbour 3.2 - bcc 7.4
User avatar
cnavarro
Posts: 6608
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Has thanked: 6 times
Been thanked: 8 times

Re: webservice implementation using php

Post 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

//----------------------------------------------------------------------------//// 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()   endifReturn 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
User avatar
Antonio Linares
Site Admin
Posts: 42739
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 105 times
Been thanked: 108 times
Contact:

Re: webservice implementation using php

Post by Antonio Linares »

Enhanced webservice:

webservice.php

Code: Select all | Expand

<?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

{    "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

#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
User avatar
cnavarro
Posts: 6608
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Has thanked: 6 times
Been thanked: 8 times

Re: webservice implementation using php

Post by cnavarro »

Also

Code: Select all | Expand

//----------------------------------------------------------------------------//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
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
User avatar
cnavarro
Posts: 6608
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Has thanked: 6 times
Been thanked: 8 times

Re: webservice implementation using php

Post by cnavarro »

With Curl

Code: Select all | Expand

#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
User avatar
gabrielgaspar.dev
Posts: 4
Joined: Thu Mar 28, 2019 1:35 pm
Location: SC, Brasil

Re: webservice implementation using php

Post 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
User avatar
cnavarro
Posts: 6608
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Has thanked: 6 times
Been thanked: 8 times

Re: webservice implementation using php

Post by cnavarro »

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
Post Reply