有什么方法可以在我的侦听器中正确迭代一个数组,该数组包含每个 PDF 的 PDF 字节数据,并将每个数据写入一个单独的文本文件?我已经为此工作了三天,只能写第一个,仅此而已。
编辑:这是我将其剥离为的新代码:
foreach($xml->DocumentPDFs->DocumentPDF as $value) {
$value = $value->PDFBytes;
$binary = base64_decode($value);
file_put_contents($xml->EnvelopeStatus->EnvelopeID . "/" . $xml->EnvelopeStatus->DocumentStatuses->DocumentStatus->Name . ".pdf", $binary);
}
我正在使用这个:
$docarr = array();
foreach($xml->DocumentPDFs as $DocumentPDFs){
foreach ($DocumentPDFs->DocumentPDF as $DocumentPDF) {
$docarr[] = $DocumentPDF->PDFBytes;
foreach ($docarr as $pdfbyte) {
$docfiles = $xml->EnvelopeStatus->EnvelopeID . "/" . $xml->EnvelopeStatus->DocumentStatuses->DocumentStatus->Name . '.txt';
$doctxt = fopen($docfiles,"w");
write($doctxt,$pdfbyte);
fclose($doctxt);
}
}
}
出于某种原因,它一直只遍历第一个块,打印它,然后停在那里。
新编辑- 所以我只是将 XML 响应加载到 simplexml_load_file 中。这是 $xml->DocumentPDFs 上的 print_r:
SimpleXMLElement Object
(
[DocumentPDF] => Array
(
[0] => SimpleXMLElement Object
(
[Name] => Corporate_Information.pdf
[PDFBytes] => JVBERi0xLjQKJfv8/f4KMiAwIG9iago8PAovTW9kRGF0ZSAo...
[DocumentType] => CONTENT
)
[1] => SimpleXMLElement Object
(
[Name] => LLC_Information.pdf
[PDFBytes] => JVBERi0xLjQKJfv8/f4KMiAwIG9iago8PAovTW9kRGF0Z...
[DocumentType] => CONTENT
)
[2] => SimpleXMLElement Object
(
[Name] => Demo Document.pdf
[PDFBytes] => JVBERi0xLjQKJfv8/f4KNiAwIG9iago8PAovUGFyZW5...
[DocumentType] => CONTENT
)
[3] => SimpleXMLElement Object
(
[Name] => CertificateOfCompletion_4b79ca0d-bbc4-4b2b-9993-295755d3b3ad.pdf
[PDFBytes] => JVBERi0xLjQKJfv8/f4KMiAwIG9iago...
[DocumentType] => SUMMARY
)
)
)
$xml->DocumentPDFs->DocumentPDF 上的 print_r:
SimpleXMLElement Object
(
[Name] => Corporate_Information.pdf
[PDFBytes] => JVBERi0xLjQKJfv8/f4KMiAwIG9iago8PAovTW9kRGF0ZSAoRDoyMDEzMD...
[DocumentType] => CONTENT
)
SimpleXMLElement Object
(
[Name] => Corporate_Information.pdf
[PDFBytes] => JVBERi0xLjQKJfv8/f4KMiAwIG9iago8PAovTW9kRGF0ZSAoRDoyMD...
[DocumentType] => CONTENT
)
SimpleXMLElement Object
(
[Name] => Corporate_Information.pdf
[PDFBytes] => JVBERi0xLjQKJfv8/f4KMiAwIG9iago8PAovTW9kRGF0ZSAoRDoyMDEzMDkx...
[DocumentType] => CONTENT
)
SimpleXMLElement Object
(
[Name] => Corporate_Information.pdf
[PDFBytes] => JVBERi0xLjQKJfv8/f4KMiAwIG9iago8PAovTW9kRGF0ZSAoR...
[DocumentType] => CONTENT
)
出于某种原因,它会正确返回 DocumentPDF,但是当我深入研究 DocumentPDF 时,它会返回基本相同的 PDF。这是为什么?
较新的编辑- 所以我只是将 foreach 调用更改为$xml->DocumentPDFs->DocumentPDF->PDFBytes as $value
,对相同的项目执行 print_r ,并且 DocumentPDFs 上的打印返回相同的内容,但 DocumentPDF 上的打印返回一个对象。据我所知,我正在正确解析。
更多新编辑- 我将我的 foreach 循环更改为:
foreach($xml->DocumentPDFs as $DocumentPDFs) {
foreach ($DocumentPDFs->DocumentPDF as $DocumentPDF) {
foreach ($DocumentPDF->PDFBytes as $pdfbytes) {
$binary = base64_decode($pdfbytes);
file_put_contents($xml->EnvelopeStatus->EnvelopeID . "/" . $xml->EnvelopeStatus->DocumentStatuses->DocumentStatus->Name . ".pdf", $binary);
}
}
}
然后我再次打印,DocumentPDF 上的那个现在打印了正确的信息。我仍然无法让它写出所有的 PDF。有什么想法吗?
最新编辑- 所以我把它变成了一个常规数组:
$json = json_encode($xml);
$array = json_decode($json,TRUE);
foreach($array['DocumentPDFs']['DocumentPDF'] as $DocumentPDF){
$binary = base64_decode($DocumentPDF['PDFBytes']);
file_put_contents($xml->EnvelopeStatus->EnvelopeID . "/" . $xml->EnvelopeStatus->DocumentStatuses->DocumentStatus->Name . ".pdf", $file);
}
但它仍然只是打印最后一个 PDF。我究竟做错了什么?!