2013-04-12 23 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没有被下载。