HMG + PHP simplexml_load_file()

HMG en Español

Moderator: Rathinagiri

User avatar
edufloriv
Posts: 238
Joined: Thu Nov 08, 2012 3:42 am
DBs Used: DBF, MariaDB, MySQL, MSSQL, MariaDB
Location: PERU

HMG + PHP simplexml_load_file()

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

Eduardo Flores Rivas


LIMA - PERU
User avatar
dragancesu
Posts: 921
Joined: Mon Jun 24, 2013 11:53 am
DBs Used: DBF, MySQL, Oracle
Location: Subotica, Serbia

Re: HMG + PHP simplexml_load_file()

Post by dragancesu »

User avatar
edufloriv
Posts: 238
Joined: Thu Nov 08, 2012 3:42 am
DBs Used: DBF, MariaDB, MySQL, MSSQL, MariaDB
Location: PERU

Re: HMG + PHP simplexml_load_file()

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

Eduardo Flores Rivas


LIMA - PERU
franco
Posts: 821
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: HMG + PHP simplexml_load_file()

Post 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
All The Best,
Franco
Canada
User avatar
apais
Posts: 440
Joined: Fri Aug 01, 2008 6:03 pm
DBs Used: DBF
Location: uruguay
Contact:

Re: HMG + PHP simplexml_load_file()

Post by apais »

Why use PHP where you can use Harbour and your dbfs as always ?
Angel Pais
Web Apps consultant/architect/developer.
franco
Posts: 821
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: HMG + PHP simplexml_load_file()

Post 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
All The Best,
Franco
Canada
User avatar
apais
Posts: 440
Joined: Fri Aug 01, 2008 6:03 pm
DBs Used: DBF
Location: uruguay
Contact:

Re: HMG + PHP simplexml_load_file()

Post 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/
Angel Pais
Web Apps consultant/architect/developer.
franco
Posts: 821
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: HMG + PHP simplexml_load_file()

Post 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
All The Best,
Franco
Canada
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: HMG + PHP simplexml_load_file()

Post 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
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
mol
Posts: 3722
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: HMG + PHP simplexml_load_file()

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