tschoffelen / php-pkpass

💳 PHP class for creating passes for Wallet on iOS.
https://includable.com
MIT License
913 stars 188 forks source link

setJSON() error when using json_encode() #20

Closed Shalling closed 11 years ago

Shalling commented 12 years ago

Due to I tried to use setJSON() by giving parameter from json_encode() and i got error on iPhone that "Safari can't download the file". So I would like to suggest some fixing for this to easily format the JSON, if it's possible.

For example <?php $json_data = array( "passTypeIdentifier" => "pass.com.example.passbook", "teamIdentifier" => "ABCDEFGHIJK", "formatVersion" => 1, "organizationName" => "Example", "serialNumber" => uniqid(), "backgroundColor" => "rgb(107,156,196)", "logoText" => "Company", "description" => "Demo Employee pass", "primaryFields" => array( array( 'key' => 'name', 'label' => 'Name', 'name' => $name, ), array( 'key' => 'lastName', 'label' => 'Last Name', 'lastName' => $lastName, ), array( 'key' => 'depart', 'label' => 'Depart', 'depart' => $depart, ), "barcode" => array( "format" => "PKBarcodeFormatQR", "message" => "Name: " . $name . " Last Name: " . $lastName . " Depart: " . $depart, "messageEncoding" => "iso-8859-1" ), ), ); $json = json_encode($json_data); $pass->setJSON($json);

?>

tschoffelen commented 12 years ago

This should usually just work fine. Are you sure that all the fields are according to the specification? I'm not totally sure about the use of uniqueid().

ptz0n commented 12 years ago

I use arrays and json_encode instead of manually formatted JSON in my application. Maybe I can push a version of the example.php file using the same method in a couple of days.

ghost commented 12 years ago

Same problem here

$passJson = json_encode($passArray, JSON_NUMERIC_CHECK); $pass->setJSON($passJson);

works fine in a browser(file does open) but on iPhone safari can't download the file.

but when i use the same json string assigned in this script - it works and the pass open !?

$passJson = '{"formatVersion":1,"serialNumber":"320193-463-386","teamIdentifier":'.....}; $pass->setJSON($passJson);

some ideas?

@ptz0n : can you please push your example?

Shalling commented 11 years ago

i found that when json_encode the array instead of primaryFields : [ { key: '', ...}, { ... }, { ... } ] it shows primaryFields: { "0" : { key: '', ... }, "1" : { ... }, "2" : { ... } }

any workaround on this issue?

ptz0n commented 11 years ago

Your array keys are not following the Passbook specification. The fields objects should always contain key, label and value keys. The following PHP array;

json_encode(array(
    'primaryFields' => array(
        array(
            'key'   => 'origin',
            'label' => 'From',
            'value' => 'Stockholm C'
        ),
        array(
            'key'   => 'destination',
            'label' => 'To',
            'value' => 'Göteborg C'
        )
    )
));

will give you:

{
    "primaryFields":[
        {
            "key": "origin",
            "label": "From",
            "value": "Stockholm C"
        },
        {
            "key": "destination",
            "label": "To",
            "value": "G\u00f6teborg C"
        }
    ]
}

If it does not. What PHP (and JSON) version are you running?

ptz0n commented 11 years ago

@Shalling I have now submitted the pull request #22 regarding JSON encode from pass data formatted as PHP arrays.

ghost commented 11 years ago

i solved my problem.

i'm not sure but i guess i had two failures.

  1. the pass must add an icon AND a logo png - if not safari can't download the pkpass file
  2. the message value must be a string. i use JSON_NUMERIC_CHECK to determine whether the php array value is a number (eg FormatVersion,Latitude... ) but in some cases i returned an int value for the barcode.
ptz0n commented 11 years ago

Great that you pointed out the JSON_NUMERIC_CHECK constant. Good job getting it working!

lock[bot] commented 6 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.