【问题标题】:Create pass for passbook in php在php中为存折创建通行证
【发布时间】:2013-03-24 22:51:47
【问题描述】:

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

如果我从终端创建通行证,它工作正常。但是在带有 PHP-PKPass 的 PHP 中。

我在下面的 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);
}

【问题讨论】:

  • 当您在设备上安装一张通行证时,您会在控制台中看到什么(要查看此内容,请将您的 iPhone 连接到 Mac,启动 Xcode 并单击窗口 -> 管理器)。
  • 在 iPhone 中我无法安装它,因为它看起来像 cupon.pkpass
  • 可能是 Safari 正在下载该文件,因为它无法将其识别为传递文件 - 您是否使用 Content-Type: application/vnd.apple.pkpass 标头提供它?
  • 我尝试通过电子邮件而不是 safari 发送它(当我在终端上创建它并完美运行时,我也通过邮件发送它)。从我自己的应用程序中添加它的想法
  • 那么是否正在创建一个附件,该附件是否正确地附加到您的电子邮件中?你是什​​么意思它看起来像cupon.pkpass

标签: php passbook


【解决方案1】:

在我尝试安装您的通行证时查看控制台输出,我看到以下警告。

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.

深入您的 pass.json,我发现您的 primaryFields 字典不包含“值”键。

    "primaryFields": [{
            "key": "key",
            "label": "30% de Descuento en Persianas"
        }
    ],

要解决此问题,请更改您的 PHP 以将 value 键添加到您的 primaryFields 字典中。

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

【讨论】:

  • 感谢男士,它完美运行我消除了“价值”,因为我不希望它出现。不过谢谢!
猜你喜欢
  • 2015-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多