Page 1 of 3

HMG + PHP simplexml_load_file()

Posted: Fri May 08, 2020 4:59 pm
by edufloriv
Saludos amigos,

Con esta cuarentena me dedique a aprender a programar con PHP y estoy desarrollando un pequeño proyecto, una pagina web que muestre la lista de precios de la empresa. Como la información a subir y bajar del host es poca, NO estoy usando base de datos en el lado del servidor.

Lo primero que hice fue extraer del servidor local la lista de clientes y pasarla a archivos individuales .XML:

Code: Select all

*-------------------------------------------------------------------------
*-------------------------------------------------------------------------
*-------------------------------------------------------------------------

PROC Clie_To_XML

LOCAL qClieX , oClieX

	Win_Utiles.StatusBar.Item(1) := 'Generando XMLs de clientes...'

   qClieX  := ;
   "select * "+;
   "from maesclie "+;
   "where clie_vend > space(25) and clie_esta = 1 and clie_cod <> '0400' and clie_cat <> 'PRV' "+;
   "order by clie_cod"

   oClieX := TOleAuto():New('ADODB.Recordset')
   oClieX:CursorLocation = adUseClient
   oClieX:Open( qClieX , oConexion , adOpenForwardOnly , adLockReadOnly )

   FOR nPX = 1 TO oClieX:RecordCount()

      aClieDire := GetDirsClie( oClieX:Fields("clie_cod"):Value )

      cCliXml := '<?xml version="1.0" encoding="UTF-8"?>'+chr(13)+chr(10)+;
                 '<cliente>'+chr(13)+chr(10)+;
                 '<codigo>'+oClieX:Fields("clie_cod"):Value+'</codigo>'+chr(13)+chr(10)+;
                 '<ruc>'+oClieX:Fields("clie_ruc"):Value+'</ruc>'+chr(13)+chr(10)+;
                 '<razonsocial>'+rtrim(oClieX:Fields("clie_nom"):Value)+'</razonsocial>'+chr(13)+chr(10)+;
                 '<cat>'+oClieX:Fields("clie_cat"):Value+'</cat>'+chr(13)+chr(10)+;
                 '<plan>'+oClieX:Fields("clie_plan"):Value+'</plan>'+chr(13)+chr(10)+;
                 '<vend>'+rtrim(oClieX:Fields("clie_vend"):Value)+'</vend>'+chr(13)+chr(10)+;
                 '<pp>'+oClieX:Fields("clie_pp"):Value+'</pp>'+chr(13)+chr(10)+;
                 '</cliente>' + chr(13)+chr(10)

      cCliXml := cCliXml + '<Direcciones>'+chr(13)+chr(10)
      FOR nCliDir = 1 TO LEN(aClieDire)
      
         cCliXml := cCliXml + ;
                    '<Direcc>'+chr(13)+chr(10)+;
                    '<DireccId>'+aClieDire[nCliDir,1]+'</DireccId>'+chr(13)+chr(10)+;
                    '<DireccDesc>'+aClieDire[nCliDir,2]+'</DireccDesc>'+chr(13)+chr(10)+;
                    '</Direcc>'+chr(13)+chr(10)

      NEXT
      cCliXml := cCliXml + '</Direcciones>'+chr(13)+chr(10)

      cFilXml := 'clientes\' + oClieX:Fields("clie_cod"):Value + '.xml'

      SET( _SET_EOF, .F. )
      HB_MEMOWRIT( cFilXml , cCliXml )
      SET( _SET_EOF, .T. )

      oClieX:MoveNext()

   NEXT

   oClieX:Close()

	Win_Utiles.StatusBar.Item(1) := 'Listo.'

RETURN
Luego subí los XML a una carpeta del host. Mi problema es que cuando trato de leerlos con PHP la funcion simplexml_load_file() no me devuelve nada. Intuyo que estoy haciendo algo mal al generar los XML. Solo por referencia tambien les copio el código de PHP que estoy usando:

Code: Select all

<?php

   session_start() ;

   include( 'funcion.php' ) ;
   
   if ( isset($_GET["frm_clicod"]) ) {

      $clie_codigo = $_GET["frm_clicod"] ;
      $clie_clave  = $_GET["frm_clipas"] ;

      if ( strlen($clie_codigo)==4 ) {

         $clie_pas = 'pases/' . $clie_codigo . '.txt' ;
         $clie_xml = 'clientes/' . $clie_codigo . '.xml' ;

         if ( file_exists($clie_pas) && file_exists($clie_xml) ) {

            $clie_lee = leetxt( $clie_pas ) ;
            $clie_key = $clie_lee[0] ;

            if ( $clie_clave == $clie_key ) {

               $clie_data = simplexml_load_file( $clie_xml );
               $clie_dirs = ARRAY() ;

               if ( $clie_data==false ) {
                  echo 'ERROR: El xml no se pudo leer' ;  //<- Siempre me lanza este error
               }
               $clie_ruc    = $clie_data->cliente->ruc ;
               $clie_nombre = $clie_data->cliente->razonsocial ;
               $clie_cat    = $clie_data->cliente->cat ;
               $clie_plan   = $clie_data->cliente->plan ;
               $clie_vend   = $clie_data->cliente->vend ;
               $clie_pp     = $clie_data->cliente->pp ;

               foreach( $clie_data->direcciones as $dirx ) {

                  $clie_dirs[] = array( $dirx->DireccId , $dirx->DireccDesc ) ;

               }

               $_SESSION["mem_clicod"]     = $clie_codigo ;
               $_SESSION["mem_cliruc"]     = $clie_ruc    ;
               $_SESSION["mem_clinom"]     = $clie_nombre ;
               $_SESSION["mem_clicat"]     = $clie_cat    ;
               $_SESSION["mem_clipla"]     = $clie_plan   ;
               $_SESSION["mem_cliven"]     = $clie_vend   ;
               $_SESSION["mem_clipp"]      = $clie_pp     ;
               $_SESSION["mem_clidir"]     = $clie_dirs   ;
               
               echo 'Acceso concedido a '.$clie_nombre.'</br>' ;
               foreach( $clie_dirs as $dirx ) {
                  echo 'Direccion: '.$dirx[1].'</br>' ;
               }

            }
         }
      }
   }

   echo "C&oacutedigo de cliente no v&aacutelido (".$fases.")" ;
   return false;

?>
Puse un verificador para $clie_data y siempre me sale el error: 'ERROR: El xml no se pudo leer' y la variable $clie_nombre llega vacia, el .prg donde genero los XML esta en formato UTF_8 SIN BOM

Alguien con experiencia en proyectos similares que por favor me tienda una mano.

Cordiales saludos,


Att.

Re: HMG + PHP simplexml_load_file()

Posted: Sat May 09, 2020 12:50 pm
by dragancesu

Re: HMG + PHP simplexml_load_file()

Posted: Sat May 09, 2020 2:55 pm
by edufloriv
Saludos amigos,

Lo resolví, lo reporto para los interesados.

Primero, el .xml estaba mal estructurado, esta es la estructura correcta:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<cliente>
  <codigo>0008</codigo>
  <ruc>20509978477</ruc>
  <razonsocial>FARMAVICTORIA SAC.</razonsocial>
  <cat>ACC</cat>
  <plan>A</plan>
  <vend>JUVITSA</vend>
  <pp>S</pp>
  <Direcc>
    <DireccId>3</DireccId>
    <DireccDesc>AV. BOLIVAR 1155 - PUEBLO LIBRE - LIMA</DireccDesc>
  </Direcc>
</cliente>
Como ven el tag <Direcc> debe estar anidado dentro del tag <cliente>, con lo que el código PHP quedaría así:

Code: Select all

<?php

   session_start() ;

   include( 'funcion.php' ) ;
   
   if ( isset($_GET["frm_clicod"]) ) {

      $clie_codigo = $_GET["frm_clicod"] ;
      $clie_clave  = $_GET["frm_clipas"] ;

      if ( strlen($clie_codigo)==4 ) {

         $clie_pas = 'pases/' . $clie_codigo . '.txt' ;
         $clie_xml = 'clientes/' . $clie_codigo . '.xml' ;

         if ( file_exists($clie_pas) && file_exists($clie_xml) ) {

            $clie_lee = leetxt( $clie_pas ) ;
            $clie_key = $clie_lee[0] ;

            if ( $clie_clave == $clie_key ) {

               $clie_data = simplexml_load_file( $clie_xml );
               $clie_dirs = ARRAY() ;

               if ( $clie_data==false ) {
                  echo 'ERROR: El xml no se pudo leer' ;
               }
               $clie_ruc    = $clie_data->ruc ;
               $clie_nombre = $clie_data->razonsocial ;
               $clie_cat    = $clie_data->cat ;
               $clie_plan   = $clie_data->plan ;
               $clie_vend   = $clie_data->vend ;
               $clie_pp     = $clie_data->pp ;

               foreach( $clie_data->Direcc as $dirx ) {

                  $clie_dirs[] = array( $dirx->DireccId , $dirx->DireccDesc ) ;

               }

               $_SESSION["mem_clicod"]     = $clie_codigo ;
               $_SESSION["mem_cliruc"]     = $clie_ruc    ;
               $_SESSION["mem_clinom"]     = $clie_nombre ;
               $_SESSION["mem_clicat"]     = $clie_cat    ;
               $_SESSION["mem_clipla"]     = $clie_plan   ;
               $_SESSION["mem_cliven"]     = $clie_vend   ;
               $_SESSION["mem_clipp"]      = $clie_pp     ;
               $_SESSION["mem_clidir"]     = $clie_dirs   ;
               
               echo 'Acceso concedido a '.$clie_nombre.'</br>' ;
               foreach( $clie_dirs as $diry ) {
                  echo 'Direccion: '.$diry[1].'</br>' ;
               }

            }
         }
      }
   }

   echo "C&oacutedigo de cliente no v&aacutelido (".$fases.")" ;
   return false;

?>
Con estos cambios, las variables llegan con los datos correctos.

Saludos y gracias por su atención.

Re: HMG + PHP simplexml_load_file()

Posted: Sat May 09, 2020 4:25 pm
by franco
Eduardo, it is odd you posted this at this time.
Yesterday I had a call from a consignment store asking if I could create on web an area their clients could see only their history.
They are happy with their software and can export a report to Excel.
I am like you and have no experience with this, but this looks just like what I could use to create something for them.
The client would log in and only see their history.
Would this work for me and does anyone have any different ideas.
Thanks ....Franco

Re: HMG + PHP simplexml_load_file()

Posted: Sat May 09, 2020 9:00 pm
by apais
Why use PHP where you can use Harbour and your dbfs as always ?

Re: HMG + PHP simplexml_load_file()

Posted: Sun May 10, 2020 4:42 am
by franco
Apais, thanks for your response.
How would this work (as I know nothing about web).
Would I build exe in hmg and put it on the web server and then the store would send a new text file or dbf created by excel export every day.
Franco

Re: HMG + PHP simplexml_load_file()

Posted: Mon May 11, 2020 4:17 am
by apais
There's an Apache module that implements Harbour as your back end language.
Here's a page I've done to keep track of all these new technologies.
https://www.mod-harbour.com/

Re: HMG + PHP simplexml_load_file()

Posted: Wed May 13, 2020 5:03 pm
by franco
Sorry Apais, I have tried to study this but do not understand.
I understand what Dragan posted.
Can I create the same thing in a folder on my computer to work just like the web.
Thanks Franco

Re: HMG + PHP simplexml_load_file()

Posted: Wed May 13, 2020 10:45 pm
by jairpinho
mod-harbou is good but very difficult to understand and put to work I agree with colleagues, I will study these codes to see how it works, I also think it is better to master hmg on the web, great job

Re: HMG + PHP simplexml_load_file()

Posted: Sun May 17, 2020 6:17 am
by mol
Some years ago I written club contributions and events management system.
First, it was working bith database saved in XML file.
I've used php, javascript and hmgscript by Roberto.
Last version uses sql database.
Please attach sample XML file and write, what do you want to do with it.
I'll try prepare warking sample