I'm trying to create a controller for a user to download a membership card as a pass in Apple Wallet. I'm looking to use the PKPass Library.
I've got things set up and working using their example like this:
$pass = new PKPass(Yii::getAlias('path/to/cert.p12'), 'myPassword');
$data = [
'description' => 'Membership Card',
// etc as per example: https://github.com/includable/php-pkpass/blob/master/examples/example.php
];
$pass->setData($data);
$pass->create(true);
but get a 'Headers already sent' error when trying to download the pass. I feel like this is because the PKPass class is outputting to the browser before my module has finished processing the request.
So changing the last line to $createdPass = $pass->create(false); and then handling the output is probably what I want to do, but I'm stuck on how to get that $zip response back into my controller as a file to download. Here is the create method:
public function create($output = false)
{
// Prepare payload
$manifest = $this->createManifest();
$signature = $this->createSignature($manifest);
// Build ZIP file
$zip = $this->createZip($manifest, $signature);
// Return pass
if (!$output) {
return $zip;
}
// Output pass
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.apple.pkpass');
header('Content-Disposition: attachment; filename="' . $this->getName() . '"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s T'));
header('Pragma: public');
echo $zip;
return '';
}
This might be more of a basic PHP question, but does anyone know how I could do that?
$outputisfalse, you're already returning the$zip, so the controller can output it. What is not working? As a sidenote, setting headers and outputting content should not happen in a utility method, it should happen in the controller. – MoritzLost Jun 30 '23 at 07:39