PHP 新文件的结果用法
PHP result usage for new file
我使用从 CSV 文件中获取的几个参数从服务器请求数据,它可以工作,但输出对用户不友好。
我怎样才能很好地打印我的 PHP 响应的结果?
这是我的脚本:
<?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)
?>
结果如下:
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
(
)
)
我得到了我想要的,但我希望能够使用每个结果的输出来填充另一个文件或类似的东西,这样我的最终用户就可以很好地看到它。我 运行 var_dump
进行调试,输出是这样的:
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) {
}
}
如何使用输出?
您可能正在遵循 this post.
中使用的方法
但是您可以直接从 XML 在 while 循环中获取值,使用简单的 XML 对象并将值存储在数组中,您可以查看有关如何访问节点的更多详细信息值 here.
一个简短的例子:
<?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,
...
];
}
在此之后,您可以使用 $arr_data 值即时生成 CSV 文件或将其保存在某个地方。
希望对您有所帮助...
正如@MarcosAM 在之前的回答中所说,直接从 XML 获取值更好。我用 $data = $xml->children('s', true)->children('', true)->children('', true)->children('a', true);
得到了我想要的 children object ,然后把我想要的值赋给变量 $estatus = $data->CodigoEstatus;
.
一旦你这样做,只需要使用 file_put_contents
并将你想要的变量分配给一个字符串变量 file_put_contents('validacion_facturas.csv', $resultado.PHP_EOL , FILE_APPEND | LOCK_EX);
我使用 _FILE_APPEND_ 继续写入同一个文件并创建脚本开头的 headers。
这是最终的脚本,如果有的话:
<?php
libxml_use_internal_errors(true);
$file = fopen("foo.csv","r");
$headers = "EMISOR,RECEPTOR,TOTAL,UUID,ESTATUS,ESTADO,CANCELABLE,ESTATUS CANCELACION";
file_put_contents('validacion_facturas.csv', $headers.PHP_EOL , FILE_APPEND | LOCK_EX);
while (($data = fgetcsv($file)) !== FALSE) {
$emisor = $data[0];
$receptor = $data[1];
$total = $data[2];
$uuid = $data[3];
$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);
if ($xml === false) {
echo "Failed loading XML: ";
foreach(libxml_get_errors() as $error) {
echo "<br>", $error->message;
}
}
else {
$data = $xml->children('s', true)->children('', true)->children('', true)->children('a', true);
$estatus = $data->CodigoEstatus;
$cancelable = $data->EsCancelable;
$estado = $data->Estado;
$estatus_cancelacion = $data->EstatusCancelacion;
}
$resultado = "$emisor,$receptor,$total,$uuid,$estatus,$estado,$cancelable,$estatus_cancelacion";
file_put_contents('validacion_facturas.csv', $resultado.PHP_EOL , FILE_APPEND | LOCK_EX);
}
fclose($file)
?>
我使用从 CSV 文件中获取的几个参数从服务器请求数据,它可以工作,但输出对用户不友好。
我怎样才能很好地打印我的 PHP 响应的结果?
这是我的脚本:
<?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)
?>
结果如下:
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
(
)
)
我得到了我想要的,但我希望能够使用每个结果的输出来填充另一个文件或类似的东西,这样我的最终用户就可以很好地看到它。我 运行 var_dump
进行调试,输出是这样的:
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) {
}
}
如何使用输出?
您可能正在遵循 this post.
中使用的方法但是您可以直接从 XML 在 while 循环中获取值,使用简单的 XML 对象并将值存储在数组中,您可以查看有关如何访问节点的更多详细信息值 here.
一个简短的例子:
<?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,
...
];
}
在此之后,您可以使用 $arr_data 值即时生成 CSV 文件或将其保存在某个地方。
希望对您有所帮助...
正如@MarcosAM 在之前的回答中所说,直接从 XML 获取值更好。我用 $data = $xml->children('s', true)->children('', true)->children('', true)->children('a', true);
得到了我想要的 children object ,然后把我想要的值赋给变量 $estatus = $data->CodigoEstatus;
.
一旦你这样做,只需要使用 file_put_contents
并将你想要的变量分配给一个字符串变量 file_put_contents('validacion_facturas.csv', $resultado.PHP_EOL , FILE_APPEND | LOCK_EX);
我使用 _FILE_APPEND_ 继续写入同一个文件并创建脚本开头的 headers。
这是最终的脚本,如果有的话:
<?php
libxml_use_internal_errors(true);
$file = fopen("foo.csv","r");
$headers = "EMISOR,RECEPTOR,TOTAL,UUID,ESTATUS,ESTADO,CANCELABLE,ESTATUS CANCELACION";
file_put_contents('validacion_facturas.csv', $headers.PHP_EOL , FILE_APPEND | LOCK_EX);
while (($data = fgetcsv($file)) !== FALSE) {
$emisor = $data[0];
$receptor = $data[1];
$total = $data[2];
$uuid = $data[3];
$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);
if ($xml === false) {
echo "Failed loading XML: ";
foreach(libxml_get_errors() as $error) {
echo "<br>", $error->message;
}
}
else {
$data = $xml->children('s', true)->children('', true)->children('', true)->children('a', true);
$estatus = $data->CodigoEstatus;
$cancelable = $data->EsCancelable;
$estado = $data->Estado;
$estatus_cancelacion = $data->EstatusCancelacion;
}
$resultado = "$emisor,$receptor,$total,$uuid,$estatus,$estado,$cancelable,$estatus_cancelacion";
file_put_contents('validacion_facturas.csv', $resultado.PHP_EOL , FILE_APPEND | LOCK_EX);
}
fclose($file)
?>