使用php将p12转换为pem文件

使用php将p12转换为pem文件,php,apple-push-notifications,Php,Apple Push Notifications,我查过了 并按照步骤从p12文件生成pem文件。下面是生成pem文件的代码 .... if ($this->file->save($uploadDirectory . $filename . '.' . $ext)) { $filenamewithpath = $uploadDirectory . $filename . '.' . $ext; $handle = fopen($filenamewithpath, 'r'); $p12buf = fread($han

我查过了 并按照步骤从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());
   }
}
....
请建议解决此问题。

我会打电话的


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

我在为iOS开发时也遇到了同样的问题。我们获得了*.cer证书,并使用以下命令行将其更改为*.pem:
openssl x509-通知der-in aps\U production\U identity.cer-out aps\U production\U identity.pem

如果这不起作用,请确保包括私钥,如下所示:

您可能还需要在pem中添加中间证书,或者最近的ca捆绑包也可以。@jack谢谢,这一个[“cert”]=>string(1996)”----开始证书------(取自$results数组)需要添加吗?拥有证书是一回事,但您需要一些东西来验证它,您可以通过google curl cacert获得一个捆绑文件download@eirikima我找到了这个,试了试,但没用。
<?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 
'openssl pkcs12  -in cert.p12  -inpass pass:password  ...something.. ..something...'