2

我检查了这个url 并按照步骤从 p12 文件生成 pem 文件。下面是生成pem文件的代码。

....
if ($this->file->save($uploadDirectory . $filename . '.' . $ext)) {
   $filenamewithpath = $uploadDirectory . $filename . '.' . $ext;
   $handle = fopen($filenamewithpath, 'r');
   $p12buf = fread($handle, filesize($filenamewithpath));
   fclose($file); 
   $password = @$p12pwd;
   $results = array();
   $worked = openssl_pkcs12_read($p12buf, $results, $password);
   //d($results); exit;
   if ($worked) {
      //echo '<pre>', print_r($results, true), '</pre>';
      $new_password = null;
      $result = null;
      $worked = openssl_pkey_export($results['pkey'], $result, $new_password);
      if($worked) {
         //echo "<pre>It worked!  Your new pkey is:\n", $result, '</pre>';
         file_put_contents( $uploadDirectory . $filename . '.pem',$result);
         return array(
            'success' => true,
            'filename'=>$filename . '.pem',
            'uploaddir' =>$uploadDirectory,
         );
      } else {
         return array('error' => openssl_error_string());
      }

   } else {
      return array('error' => openssl_error_string());
   }
}
....

我让它工作正常,生成的 pem 文件存储在给定的目录中。现在我正在尝试使用这个 pem 文件进行推送通知。检查下面的代码,

<?php 
   $apnsHost = 'gateway.sandbox.push.apple.com';
   //$apnsHost = 'gateway.push.apple.com';

   $apnsCert = 'test.pem';
   $apnsPort = 2195;

   $streamContext = stream_context_create(); 
   stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

   $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
   $payload['aps'] = array('alert' => 'this is test!', 'badge' => 1, 'sound' => 'default');
   $output = json_encode($payload);
   $token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
   $token = pack('H*', str_replace(' ', '', $token)); 
   $apnsMessage = chr(0) . chr(0) . chr(32) . $token . chr(0) . chr(strlen($output)) . $output;
   //var_dump($apnsMessage); exit;
   fwrite($apns, $apnsMessage);

   @socket_close($apns);
   fclose($apns);
?>

当我运行此代码时,出现以下错误,

Warning: stream_socket_client(): Unable to set local cert chain file `test.pem'; Check that your cafile/capath settings include details of your certificate and its issuer in /var/www/html/myserver/apns/test.php on line 13
Warning: stream_socket_client(): failed to create an SSL handle in /var/www/html/myserver/apns/test.php on line 13 
Warning: stream_socket_client(): Failed to enable crypto in /var/www/html/ela/apns/test.php on line 13 
Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /var/www/html/myserver/apns/test.php on line 13 
Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/html/myserver/apns/test.php on line 20 
Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/myserver/apns/test.php on line 23 

请建议解决此问题。

4

2 回答 2

1

我会打电话给

'openssl pkcs12  -in cert.p12  -inpass pass:password  ...something.. ..something...'

在命令行上并尝试获取或通过管道返回其输出。也许在 php 中有一个包装器,否则系统命令似乎是最简单的方法。

于 2012-08-17T10:02:24.857 回答
0

在为 iOS 开发时,我遇到了同样的问题。我们获得了 *.cer 证书并使用以下命令行将它们更改为 *.pem:
openssl x509 -inform der -in aps_production_identity.cer -out aps_production_identity.pem

如果这不起作用,请确保包含此处所示的私钥: Apple Push Notification Service

于 2012-08-30T02:16:12.173 回答