Passbook - JSON中的硬编码位置可以工作但不能传递变量

时间:2015-03-16 22:15:06

标签: php html arrays json passbook

我正在使用预先构建的here结构来创建Apple Passbook通行证。我正在使用星巴克示例,但参数不同。

背景: Apple只允许将10个地点保存到通行证中。保存位置的目的是在您附近时触发电话通知。

我的系统流程是:

  • 用户输入家庭住址
  • google检索精确坐标lat / lng
  • lat和lng设置为表单的隐藏输入,并通过POST来传递创建PHP。
  • 服务器查询10个最近的位置,并将其位置添加到传递JSON。
  • 传递创建和下载

问题

这可能是PHP或Passbook的问题。

在Mysql中发布变量查询就好了。在测试JSON时,JSON匹配下面的文档示例中给出的目标模式。 10个最近的商店确实以正确的语法插入自己。但是,在JSON中将其位置硬编码的通行证

    'locations' => array(

        array(
            'longitude' => 123456,
            'latitude' => 123456,
            'relevantText' => 'You are near store'
            )
        )

这些会显示在地图位置。

通过任何方式为其位置设置变量的通行证,例如

    'locations' => array(

        array(
            'longitude' => $lng,
            'latitude' => $lat,
            'relevantText' => 'You are near store'
            )
        )

不在地图上显示其位置。 我试过混合两种方法,果然,只有硬编码的位置出现 - 变量提交的lats和lng被忽略。

在最后一次传递创建步骤之前,我得到一个位置公式,如:

"locations":[
{"longitude":-XX.XXXX,"latitude":XX.XXXX,"relevantText":"PLACE NAME"},
...(ten times.. comma'd until last one)...
]

可与apple's documentation中的目标公式进行比较:

"locations" : [
    {"latitude" : 37.3229, "longitude" : -122.0323, "relevantText" : "Store nearby on 3rd and Main."}
]

我不明白为什么变量可以正常传递,查询正常,输入JSON就好了,但要区分传递文件的作用。


编辑:有关请求的更多信息。 传递来自谷歌地理编码的lat / lng索引的HTML表单。

<form action="pass.php" method="post">   
        <fieldset>       
        <input type='text' id='lat' name='lat' value='' > //set thru jquery
        <input type='text' id='lng' name='lng' value='' > //set thru jquery      
        <input type="submit" class="btn primary" name='pass' id='passButton' value=" Create pass &gt; " disabled />
        </fieldset>
</form>

生成pass的pass.php:

<?php
require('PKPass/PKPass.php');

$lat = floatval($_POST['lat']);
$lng = floatval($_POST['lng']);

require_once ('../../functions.php'); //get mysql wrapper class


$query = "SELECT *, ( 3959 * acos( cos( radians($lat) ) * cos( radians( latitude ) ) * 
cos( radians( longitude ) - radians($lng) ) + sin( radians($lat) ) * 
sin( radians( latitude ) ) ) ) AS distance FROM dealers HAVING
distance < 25 ORDER BY distance LIMIT 0 , 10;";

$result = $db->doQuery($query); //wrapper class helps do queries
$pass = new PKPass\PKPass();


$pass->setCertificate('cert/pass.p12');
$pass->setCertificatePassword('PASSWORD');
$pass->setWWDRcertPath('cert/AppleWWDRCA.pem');

// Top-Level Keys 
$standardKeys         = array(
    'description'        => 'Demo pass',
    'formatVersion'      => 1,
    'organizationName'   => 'NAME',
    'passTypeIdentifier' => 'pass.place.coupon',
    'serialNumber'       => '123456',
    'teamIdentifier'     => 'IDENTIFIER'
);
$associatedAppKeys    = array();
$relevanceKeys        = array(
    'locations' => array(

    )  

);

while ($row = $result->fetch_object()) {

    $array = 
            array(
         'longitude' => sprintf("%.4f", floatval($row->longitude)),
         'latitude' => sprintf("%.4f",floatval($row->latitude)),
        'relevantText' => $row->name
   );

    $relevanceKeys['locations'][] = $array;

}


$styleKeys            = array(

    'coupon' => array(
    'primaryFields' => array(
      array(
        'key' => 'offer',
        'label' => 'ITEM',
        'value' => 'FREE'
      )
    ),

    'auxiliaryFields' => array(
      array(
        'key' => 'expires',
        'label' => 'EXPIRES',
        'value' => '2018-04-24T10:00-05:00',
        'isRelative' => true,
        'dateStyle' => 'PKDateStyleShort'
      )
    )
  )
);

$visualAppearanceKeys = array(
    'barcode'         => array(
        'format'          => 'PKBarcodeFormatPDF417',
//        'format'          => 'PKBarcodeFormatQR',
        'message'         => 'MESSAGE',
        'messageEncoding' => 'iso-8859-1'
    ),
//    'foregroundColor' => 'rgb(255, 255, 255)',
    'backgroundColor' => 'rgb(39,81,154)',
    'logoText'        => 'COMPANY'
);
$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));

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

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

大写的明显事实是遗漏。

结果JSON:

        {"description":"Demo pass","formatVersion":1,"organizationName":"NAME","passTypeIdentifier":"pass.place.coupon","serialNumber":"123456","teamIdentifier":"IDENTIFIER","locations":[{"longitude":"-xx.xxxx","latitude":"xx.xxxx","relevantText":"STORE_1"},{"longitude":"-xx.xxxx","latitude":"xx.xxxx","relevantText":"STORE_2"},{"longitude":"-xx.xxxx","latitude":"xx.xxxx","relevantText":"STORE_3"},{"longitude":"-xx.xxxx","latitude":"xx.xxxx","relevantText":"STORE_4"},{"longitude":"-xx.xxxx","latitude":"xx.xxxx","relevantText":"STORE_5"},{"longitude":"-xx.xxxx","latitude":"xx.xxxx","relevantText":"STORE_6"},{"longitude":"-xx.xxxx","latitude":"xx.xxxx","relevantText":"STORE_7"},{"longitude":"-xx.xxxx","latitude":"xx.xxxx","relevantText":"STORE_8"},{"longitude":"-xx.xxxx","latitude":"xx.xxxx","relevantText":"STORE_9"},{"longitude":"-xx.xxxx","latitude":"xx.xxxx","relevantText":"STORE_10"}],"coupon":{"primaryFields":[{"key":"offer","label":"ITEM","value":"FREE"}],"auxiliaryFields":[{"key":"expires","label":"EXPIRES","value":"2018-04-24T10:00-05:00","isRelative":true,"dateStyle":"PKDateStyleShort"}]},"barcode":{"format":"PKBarcodeFormatPDF417","message":"MESSAGE","messageEncoding":"iso-8859-1"},"backgroundColor":"rgb(39,81,154)","logoText":"COMPANY"}

显然,xx.xxxx值是实数。它们是字符串或数字的事实似乎并不重要。当我硬编码它们时,它们就是字符串。 上面的JSON不会生成带有地图位置的传递文件,如果lat / lng是硬编码的话。 在PassWallet上测试,有效。在iO6模拟器上,它说“Safari无法下载此文件”。当我以前使用过它时。

2 个答案:

答案 0 :(得分:1)

通过附加信息,您的问题很明显。

  

它们是字符串或数字的事实似乎没有   重要的。

实际上对于Passbook来说,这很重要。 Passbook在类型上非常挑剔。 sprintf("%.4f", floatval($row->longitude))将查询结果转换为字符串,其中包格式需要加倍。

来自Passbook Package Format Reference

enter image description here

无需格式化到任何特定数量的地方,只需输入原始浮动值即可。您应该没问题。

答案 1 :(得分:0)

原来这是一个奇怪的范围错误。

所有计算和创建JSON似乎都很好,但在设置JSON和创建传递之间的某个地方,它将不再具有完整的POST变量。它们默认为0/0,非洲附近。

我能够通过使用会话变量而不是GET / POST来解决这个问题。在表单提交之前,我将jquery ajax设置会话变量,然后在pass create页面中调用它们,用它做任何计算,我需要得到10个额外的lat / lngs。如果起始变量不再存在,则无法导出其余变量!

如果有人遇到同样的问题,请发布此处。它可能是这个项目的独特之处,但它就是这样的。