I need to reponse to a client with both some binary data (a PDF file) and some additional data as JSON
How is this possible?
I can do this to send the PDF back to the client, but how to send the JSON string in the same response?
The call is done via an API so there is not a browser in the other end
header('Content-Type: '.$type);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: '.strlen($body));
echo $body;
You could use the multipart technique like in emails. One part is the JSON (text/json), the other part is the PDF (application/octet-stream a.k.a. binary).
Another possibility would be as a custom header (e.g. X-MyJSON), if the JSON string is small enough to fit into the header line. The PDF is unlikely to fit into a header string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With