用php创建存折通行证

用php创建存折通行证,php,passbook,Php,Passbook,我正在尝试为我的应用程序动态创建优惠券。我有一个PHP服务器来创建它们。但我不知道他们为什么不在存折上工作 如果我从终端创建过程,它可以正常工作。但是在PHP中使用PHP PKPass 我在下面留下了PHP代码,它出现在我的计算机中,但它没有添加到存折中(如果我通过电子邮件发送它也可以) 我100%确定passTypeIdentifier、teamIdentifier、Certificate和WWDR是100%正确的 注意:所有图像都存在 <?php include ("conexion.

我正在尝试为我的应用程序动态创建优惠券。我有一个PHP服务器来创建它们。但我不知道他们为什么不在存折上工作

如果我从终端创建过程,它可以正常工作。但是在PHP中使用PHP PKPass

我在下面留下了PHP代码,它出现在我的计算机中,但它没有添加到存折中(如果我通过电子邮件发送它也可以)

我100%确定passTypeIdentifier、teamIdentifier、Certificate和WWDR是100%正确的

注意:所有图像都存在

<?php
include ("conexion.php");
require('passpk/PKPass.php');

if (isset($_GET['cupon']) and $_GET['cupon'] != '' and $_GET['cupon'] > 0) {

    $cuponID = $_GET['cupon'];


    $pass = new PKPass\PKPass();

    $pass->setCertificate('./Certificate.p12');  // 2. Set the path to your Pass Certificate (.p12 file)
    $pass->setCertificatePassword('*******');     // 2. Set password for certificate
    $pass->setWWDRcertPath('./WWDR.pem'); // 3. Set the path to your WWDR Intermediate certificate (.pem file)


    // Top-Level Keys http://developer.apple.com/library/ios/#documentation/userexperience/Reference/PassKit_Bundle/Chapters/TopLevel.html
    $standardKeys         = array(
        'description'        => 'Store',
        'formatVersion'      => 1,
        'organizationName'   => 'Store',
        'passTypeIdentifier' => 'pass.store.store', // 4. Set to yours
        'serialNumber'       => $cupon['id'],
        'teamIdentifier'     => '********'           // 4. Set to yours
    );
    $associatedAppKeys    = array();
    $relevanceKeys        = array();
    $styleKeys            = array(
        'coupon' => array(
            'primaryFields' => array(
                array(
                    'key'   => 'key',
                    'label' => "Label"
                )
            ),
            'secondaryFields' => array(
                array(
                    'key'   => 'name',
                    'label' => 'Tienda',
                    'value' => "Name"
                ),
                array(
                    'key'   => 'date',
                    'label' => 'Válido hasta',
                    'value' => "Vigencia"
                )
            ),
            'backFields' => array(
                array(
                    'key'   => 'tienda',
                    'label' => 'Tienda',
                    'value' => "tienda"
                ),
                array(
                    'key'   => 'sucursales',
                    'label' => 'Sucursales',
                    'value' => 'Valido en las sucursales y sus horarios'
                ),
                array(
                    'key'   => 'description',
                    'label' => 'Descripción',
                    'value' => "descr"
                ),
                array(
                    'key'   => 'terms',
                    'label' => 'Términos y Condiciones',
                    'value' => "cupon"
                )
            )
        )
    );
    $visualAppearanceKeys = array(
        'barcode'         => array(
            'format'          => 'PKBarcodeFormatPDF417',
            'message'         => "cupon",
            'messageEncoding' => 'iso-8859-1'
        ),
        'foregroundColor' => 'rgb(255, 255, 255)',
        'backgroundColor' => 'rgb(4, 148, 203)',
        'logoText'        => 'cupon'
    );
    $webServiceKeys       = array();

    // Merge all pass data and set JSON for $pass object
    $passData = array_merge(
        $standardKeys,
        $associatedAppKeys,
        $relevanceKeys,
        $styleKeys,
        $visualAppearanceKeys,
        $webServiceKeys
    );

    $pass->setJSON(json_encode($passData));

    //creating a temp file called strip.png
    //generamos un directorio temporal y creamos el strip
    $uniqID = uniqid('', true);
    $dir = './tempDir/'.$uniqID;
    mkdir($dir, 0777);

    //copiamos el archvio al nuevo directorio
    copy('../'.$img, './tempDir/'.$uniqID.'/strip.png');

    // Add files to the PKPass package
    $pass->addFile($dir.'/strip.png');
    $pass->addFile('images/icon.png');
    $pass->addFile('images/icon@2x.png');
    $pass->addFile('images/logo.png');
    $pass->addFile('images/logo@2x.png');

    if(!$pass->create(true)) { // Create and output the PKPass
        echo 'Error: '.$pass->getError();
    }

    //borramos el folder temp
    unlink($dir.'/strip.png');
    rmdir($dir);
}

当我试图安装您的pass时,查看控制台输出,我看到以下警告

Mar 25 10:45:40 iPhone MobileSafari[279] <Warning>: Invalid data error reading pass pass.cuponice.cuponice/9. Pass dictionary must contain key 'value'.
Mar 25 10:45:40 iPhone MobileSafari[279] <Warning>: PassBook Pass download failed: The pass cannot be read because it isn't valid.
要修复此问题,请更改PHP,将
键添加到
主字段
字典中

$styleKeys = array(
    'coupon' => array(
        'primaryFields' => array(
            array(
                'key'   => 'key',
                'label' => "Label",
                'value' => ""
            )
        ),                            //...

当您在设备上安装一个Pass时,您会在控制台中看到什么(要看到这一点,请将iPhone连接到Mac,启动Xcode并单击“窗口->管理器”)。在iPhone中,我无法安装它,因为它看起来像cupon.pkpass。可能是Safari正在下载该文件,因为它无法识别该文件为pass文件-您是否使用
内容类型:application/vnd.apple.pkpass
标题提供该文件?我尝试通过电子邮件而不是Safari发送该文件(当我在终端上创建它时,我也会通过邮件发送它,效果非常好)。这个想法是从我自己的应用程序中添加它。是否正在创建一个附件,并且该附件是否正确地附加到您的电子邮件中?您是什么意思?它看起来像
cupon.pkpass
?谢谢大家,它工作非常好,我省略了“值”因为我不想让它出现。但是谢谢!
$styleKeys = array(
    'coupon' => array(
        'primaryFields' => array(
            array(
                'key'   => 'key',
                'label' => "Label",
                'value' => ""
            )
        ),                            //...