Is there any way to properly iterate over an array in my listener that contains the PDF byte data for each PDF and have it write each one to a separate text file? I've been working on this for three days and can only get it to write the first one and that's it.
EDIT: Here is the new code I've stripped it down to:
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);
}
I was using this:
$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);
}
}
}
For some reason it keeps just iterating over the first chunk, prints it, then stops there.
NEW EDIT - So I'm just loading the XML response into simplexml_load_file. This is the print_r on $xml->DocumentPDFs:
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
)
)
)
The print_r on $xml->DocumentPDFs->DocumentPDF:
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
)
For some reason it returns the DocumentPDFs correctly, but when I drill down into DocumentPDF it returns basically the same PDF. Why is that?
NEWER EDIT - So I just changed the foreach call to $xml->DocumentPDFs->DocumentPDF->PDFBytes as $value
, did a print_r on the same items and the print on DocumentPDFs returns the same thing but the print on DocumentPDF returns one object. I'm parsing correctly as far as I can see.
MORE NEW EDIT - I changed my foreach loop to this:
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);
}
}
}
Then I did my prints again and the one on DocumentPDF now prints the proper information. I still can't get it to write out all the PDFs though. Any thoughts?
LATEST EDIT - So I made it a regular array:
$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);
}
But it's still just printing that last PDF. What am I doing wrong?!