I am requesting data from a server using couple of parameters that I take from a CSV file and it works but the output is not user-friendly.
How can I print nicely the result of my PHP response?
This is my script:
<?php $file = fopen("fac_vig2.csv","r"); while (($data = fgetcsv($file)) !== FALSE) { $emisor = $data[0]; $receptor = $data[1]; $total = $data[2]; $uuid = $data[3]; echo "FACTURA $uuid"; $soap = sprintf('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"><soapenv:Header/><soapenv:Body><tem: Consulta><tem:expresionImpresa>?re=%s&rr=%s&tt=%s&id=%s</tem:expresionImpresa></tem:Consulta></soapenv:Body></soapenv:Envelope>', $emisor,$receptor,$total, $uuid); //encabezados $headers = [ 'Content-Type: text/xml;charset=utf-8', 'SOAPAction: http://tempuri.org/IConsultaCFDIService/Consulta', 'Content-length: '.strlen($soap) ]; $url = 'https://consultaqr.facturaelectronica.sat.gob.mx/ConsultaCFDIService.svc'; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $soap); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $res = curl_exec($ch); curl_close($ch); $xml = simplexml_load_string($res); $data = $xml->children('s', true)->children('', true)->children('', true); $data = json_encode($data->children('a', true), JSON_UNESCAPED_UNICODE); print_r(json_decode($data)); } fclose($file) ?>
And the result of it is the following:
PHP Warning: PHP Startup: Unable to load dynamic library 'xmlrpc' (tried: /usr/lib/php/20170718/xmlrpc (/usr/lib/php/20170718/xmlrpc: cannot open shared object file: No such file or directory), /usr/lib/php/20170718/xmlrpc.so (/usr/lib/php/20170718/xmlrpc.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 FACTURA 34243316-AD33-493A-A41C-6ABC94D67EA4stdClass Object ( [CodigoEstatus] => S - Comprobante obtenido satisfactoriamente. [EsCancelable] => Cancelable con aceptación [Estado] => Vigente [EstatusCancelacion] => stdClass Object ( ) ) FACTURA 9DA24941-ACD0-4640-B9EC-DEE508867779stdClass Object ( [CodigoEstatus] => S - Comprobante obtenido satisfactoriamente. [EsCancelable] => Cancelable con aceptación [Estado] => Vigente [EstatusCancelacion] => stdClass Object ( ) ) FACTURA CEBB0FEE-9FA5-413B-A333-57085FFBD881stdClass Object ( [CodigoEstatus] => S - Comprobante obtenido satisfactoriamente. [EsCancelable] => Cancelable con aceptación [Estado] => Vigente [EstatusCancelacion] => stdClass Object ( ) )
I get what I am asking for but I want to be able to use the output of each result to populate another file or something like that so my final user can just see it nicely. I ran var_dump
to debug and the output is this:
PHP Warning: PHP Startup: Unable to load dynamic library 'xmlrpc' (tried: /usr/lib/php/20170718/xmlrpc (/usr/lib/php/20170718/xmlrpc: cannot open shared object file: No such file or directory), /usr/lib/php/20170718/xmlrpc.so (/usr/lib/php/20170718/xmlrpc.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 FACTURA 34243316-AD33-493A-A41C-6ABC94D67EA4object(stdClass)#3 (4) { ["CodigoEstatus"]=> string(44) "S - Comprobante obtenido satisfactoriamente." ["EsCancelable"]=> string(26) "Cancelable con aceptación" ["Estado"]=> string(7) "Vigente" ["EstatusCancelacion"]=> object(stdClass)#4 (0) { } } FACTURA 9DA24941-ACD0-4640-B9EC-DEE508867779object(stdClass)#4 (4) { ["CodigoEstatus"]=> string(44) "S - Comprobante obtenido satisfactoriamente." ["EsCancelable"]=> string(26) "Cancelable con aceptación" ["Estado"]=> string(7) "Vigente" ["EstatusCancelacion"]=> object(stdClass)#2 (0) { } } FACTURA CEBB0FEE-9FA5-413B-A333-57085FFBD881object(stdClass)#2 (4) { ["CodigoEstatus"]=> string(44) "S - Comprobante obtenido satisfactoriamente." ["EsCancelable"]=> string(26) "Cancelable con aceptación" ["Estado"]=> string(7) "Vigente" ["EstatusCancelacion"]=> object(stdClass)#1 (0) { } }
How can I use the output?
Advertisement
Answer
Probably you are following the method used in this post.
But you can get the values directly from XML inside the while loop, using the Simple XML object and store the values in an array, you can see more details on how to access node’s values here.
A little short example:
<?php $file = fopen("fac_vig2.csv","r"); $arr_data = []; while (($data = fgetcsv($file)) !== FALSE) { ... $data = $xml->children('s', true)->children('', true)->children('', true); $data = $data->children('a', true); $arr_data[] = [ 'CodigoEstatus' => $data->CodigoEstatus, 'EsCancelable' => $data->EsCancelable, ... ]; }
After this, you can generate a CSV file on-the-fly or save it in some place, using the $arr_data values.
I hope it helps in something…