2013-04-12 52 views
0

我有一個getPass.php,它創建一個Pass實例。下面是代碼:如何在iOS中調用一個函數用於iOS存摺文件下載

//create new pass instance 
$coupon = new Pass("pass/source"); 

//fill in dynamic data 
$coupon->content['serialNumber'] = (string)uniqid(); 
$coupon->content['coupon']['secondaryFields'][0]['value'] = 
    (string)$_POST['name']; 
$coupon->content['locations'][0] = 
    $locations[(int)$_POST['location']]; 

$coupon->writePassJSONFile(); 

$coupon->writeRecursiveManifest(); 
$coupon->writeSignatureWithKeysPathAndPassword("pass", '12345'); 
$fileName = $coupon->writePassBundle(); 

echo "File saved to - $fileName"; 

**/* THIS IS WHERE IM TRYING TO PRESENT IT */** 
$this->outputPassBundleAsWebDownload($fileName); 

然後Pass.php做了很多的東西,它的作品,因爲香港專業教育學院用它收發郵件的工作通行證,但林與演示/下載功能取代了電子郵件功能。所以這裏是Pass.php的代碼:

<?php 
class Pass { 
    private $workFolder = null; 
    private $ID = null; 

    var $content = null; 
    var $passBundleFile = null; 

    private function copySourceFolderFilesToWorkFolder($path) { 
     //recurse over contents and copy files 
     $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($path), 
        RecursiveIteratorIterator::SELF_FIRST); 

     foreach($files as $name => $fileObject){ 
     if (is_file($name) && 
      substr($fileObject->getFileName(), 0, 1)!=".") { 

      copy($name, 
      $this->workFolder."/".str_replace($path."/", "",$name)); 
     } else if (is_dir($name)) { 
      mkdir($this->workFolder."/". 
       str_replace($path."/", "",$name)); 
     } 
     } 
    } 

    //import a json file into the object 
    function readPassFromJSONFile($filePath) { 
     //read the json file and decode to an object 
     $this->content = 
     json_decode(file_get_contents($filePath),true); 
    } 

    //export a json file from the object 
    function writePassJSONFile() { 
     file_put_contents($this->workFolder."/pass.json", 
         json_encode($this->content)); 
    } 

    //generate the manifest file 
    function writeRecursiveManifest() { 
     //create empty manifest 
     $manifest = new ArrayObject(); 

     //recurse over contents and build the manifest 
     $files = new RecursiveIteratorIterator(
       new RecursiveDirectoryIterator($this->workFolder), 
       RecursiveIteratorIterator::SELF_FIRST); 

     foreach($files as $name => $fileObject){ 
     if (is_file($name) && 
       substr($fileObject->getFileName(), 0, 1)!=".") { 

      $relativeName = str_replace($this->workFolder. 
             "/","",$name); 

      $sha1 = sha1(file_get_contents(
         $fileObject->getRealPath() 
       )); 
      $manifest[$relativeName] = $sha1; 
     } 
     } 

     //printf debug 
     // print_r($manifest); 

      //write the manifest file 
     file_put_contents($this->workFolder."/manifest.json", 
          json_encode($manifest)); 
    } 

    //generate the bundle signature 
    function writeSignatureWithKeysPathAndPassword($keyPath, $pass) { 
     $keyPath = realpath($keyPath); 


      if (!file_exists($keyPath.'/WWDR.pem')) 
      die("Save the WWDR certificate as 
       $keyPath/WWDR.pem"); 

     if (!file_exists($keyPath.'/passcertificate.pem')) 
     die("Save the pass certificate as 
      $keyPath/passcertificate.pem"); 

     if (!file_exists($keyPath.'/passkey.pem')) 
     die("Save the pass certificate key as 
      $keyPath/passkey.pem"); 

     $output = shell_exec("openssl smime -binary -sign". " -certfile '".$keyPath."/WWDR.pem'". 
      " -signer '".$keyPath."/passcertificate.pem'". 
      " -inkey '".$keyPath."/passkey.pem'". 
      " -in '".$this->workFolder."/manifest.json'". 
      " -out '".$this->workFolder."/signature'". 
      " -outform DER -passin pass:'$pass'"); 
    } 

     //signature debugging 
     //print(file_get_contents($this->workFolder."/signature")); 

    //create the zip bundle from the pass files 
    function writePassBundle() { 
     //1 generate the name for the .pkpass file 
     $passFile = $this->workFolder."/".$this->ID.".pkpass"; 

     //2 create Zip class instance 
     $zip = new ZipArchive(); 
     $success = $zip->open($passFile, ZIPARCHIVE::OVERWRITE); 
     if ($success!==TRUE) die("Can't create file $passFile"); 

     //3 recurse over contents and build the list 
     $files = new RecursiveIteratorIterator(
       new RecursiveDirectoryIterator($this->workFolder), 
       RecursiveIteratorIterator::SELF_FIRST); 

     //4 add files to the archive 
     foreach($files as $name => $fileObject){ 
     if (is_file($name) && 
      substr($fileObject->getFileName(), 0, 1)!=".") { 

      $relativeName = str_replace($this->workFolder."/", 
             "",$name); 
      $zip->addFile($fileObject->getRealPath(), $relativeName); 
     } 
     } 

     //5 close the zip file 
     $zip->close(); 

     //6 save the .pkpass file path and return it too 
     $this->passBundleFile = $passFile; 
     return $passFile; 
    } 

    //make new instance from a source folder 
    function __construct($path) { 
     assert(file_exists($path."/pass.json")); 

     $this->ID = uniqid(); 

     $this->workFolder = sys_get_temp_dir()."/".$this->ID; 
     mkdir($this->workFolder); 
     assert(file_exists($this->workFolder)); 

     $this->copySourceFolderFilesToWorkFolder($path); 

     $this->readPassFromJSONFile($this->workFolder."/pass.json"); 
    } 

    //delete all auto-generated files in the temp folder 
    function cleanup() 
    { 
     //recurse over contents and delete files 
     $files = new RecursiveIteratorIterator(
       new RecursiveDirectoryIterator($this->workFolder), 
       RecursiveIteratorIterator::CHILD_FIRST); 

     foreach($files as $name => $fileObject){ 
     if (is_file($name)) { 
      unlink($name); 
     } else if (is_dir($name)) { 
      rmdir($name); 
     } 
     } 

     rmdir($this->workFolder); 
    } 

    //cleanup the temp folder on object destruction 
    function __destruct() { 
     $this->cleanup(); 
    } 

     **/* THIS I ADDED AS A NEW FUNCTION TO PRESENT/DOWNLOAD */ 
     function outputPassBundleAsWebDownload($fileName) { 
     //dump the generated pass to the browser 
     header("Content-Type: application/vnd.apple.pkpass"); 
     header("Content-Disposition: attachment; ". 
     "filename=".basename($this->$fileName)); 
     header("Content-Transfer-Encoding: binary"); 
     header("Content-Length: ". filesize($this->$fileName)); 
     flush(); 
     readfile($this->$fileName);** 
} 
} 

?> 

事情是通過不被呈現。它正在被正確創建,因爲我使用fileName獲得了屏幕上的回顯。我在想什麼?我從getPass.php調用輸出函數並傳入$ fileName。爲什麼它不工作?

回答

1

您需要註釋掉回顯文件名的那一行。向瀏覽器回覆內容會迫使PHP自動生成文本輸出的標題,以便它可以提供文件名稱的文本。

一旦輸出到瀏覽器已經開始,你不能發送額外的頭。這就是爲什麼你的輸出pass bundle函數中的頭文件被忽略,並且你的pass沒有被下載。