Collect xml data in php

1

Good afternoon, I'm making a website to send sms to customers, so far I have managed to use the api that provides the sms service contracted, it would be something like this:

<script>

function enviar(telefono,mensaje){
            //document.getElementById("frame_mensaje").src = "https://comunik.jmeservicios.com/api/sms?api_key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx="+telefono+"&from=XXXXXX&message="+mensaje+"&gsm7=1";
        }

</script>

<iframe src="" name="frame_mensaje" id="frame_mensaje"></iframe>

... to simplify, let's say we have a frame with the src in white, that when clicking on a table, it returns the phone and the message and loads them in the send function, which changes the src of the frame. So all right, the SMS is sent and the frame loads me an answer xml.

The API also allows me to add (& callback_url = http% 3A% 2F% 2Fwww.urldecallback.com% 3Fmy_delivery% 3D333444), which I understand could be a php that collects the xml response, which comes in this format:

<sms>

<status>SENT</status>

<credits>1</credits>

<remain>99952</remain>

<messages>

<message     jme_id="xxxxxxx-xxxxxx-xxxxx-x-xxxxxxx"     to="+34xxxxxxxxx" status="SENT" type="sms"     credits="1" sms_count="1"     send_date="2015-01-12T12:00:59"/>

</messages>

</sms>

The question is: How do I convert this response into php variables ?. I have searched for information and with the function "simplexml_load_string ($ xml_file)" I can separate the xml data and assign them to variables, but how do I load the xml in "$ xml_file"? How should I start that php file that collects the server response ?

I "defend" a bit in javascript and php ... but the xml is something new for me. Thank you very much for the help.

    
asked by user3219178 23.11.2017 в 17:24
source

2 answers

1

Thanks to the response of Jose Reynel I have been able to find a solution, I hope that someone will help you too:

- Main page (index.php):

among other things ....

<form id="form_mensaje" class="form-horizontal form-group-sm" method="post" action="respuesta.php" role="form" target="frame_mensaje">

    (aquí vendría el contenido que queremos pasar a la url)

    </form>

<iframe name="frame_mensaje" id="frame_mensaje"></iframe>
  • File "response.php":
> <?php 
> 
> $pagina_inicio = file_get_contents("url de la web que nos genera el
> xml");
> 
> $xml = simplexml_load_string($pagina_inicio);
> 
> $estado = $xml->status;
> 
> echo $estado;
> 
> ?>

We can now give shape to the file "respuesta.php", we have the data saved in variables.

    
answered by 23.11.2017 в 21:55
0

You can do a function that returns an array with the data that you require from the xml in the following way:

<?php // librería que lee tu XML podria llamarse para el ejemplo script.php
ini_set("allow_url_fopen", 1); 
libxml_disable_entity_loader(false);
$arrTAGS = [];  


    $path = "path";//tu Path o ruta del XML, donde se encuentra el archivo .xml

    $xml = simplexml_load_file($path, 'SimpleXMLElement',LIBXML_NOWARNING);

if (is_array($xml) || is_object($xml) && !empty($xml))
    foreach ($xml as $nodo) 
    {
        $tagXML1          = $xml->tagXML1;  
        $tagXML2          = $xml->tagXML2;
        $tagXML3          = $xml->tagXML3;
        $tagXML4          = $xml->tagXML4;
        $tagXML5          = $xml->tagXML5;

        $arrTAGS["tagXML1"] = $tagXML1."";
        $arrTAGS["tagXML2"] = $tagXML2."";
        $arrTAGS["tagXML3"] = $tagXML3."";
        $arrTAGS["tagXML4"] = $tagXML4."";
        $arrTAGS["tagXML5"] = $tagXML5."";

        break;
    }
}

return $arrTAGS;

?>

to pick up the values somewhere where your library is called would be something like this:

<?php // sitio donde requieres que sea leida tu informacion del xml

    require_once('tuScript.php');

    $tagXML1 = (string)$arrTAGS["tagXML1"];
    $tagXML2 = (string)$arrTAGS["tagXML2"];
    $tagXML3 = (string)$arrTAGS["tagXML3"];
    $tagXML4 = (string)$arrTAGS["tagXML4"];
    $tagXML5 = (string)$arrTAGS["tagXML5"];

?>

I hope you find it useful, greetings.

    
answered by 23.11.2017 в 17:35