2012-08-17 58 views
2

我检查了这个url 并遵循了从p12文件生成pem文件的步骤。以下是生成pem文件的代码。p12使用php的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 

请咨询来解决这个问题。

+0

您可能还需要在pem中添加中间证书,或者最近的ca套件也可以。 – 2012-08-17 08:32:40

+0

@jack谢谢,这一[[cert]] => string(1996)“----- BEGIN CERTIFICATE -----(取自$ results数组)需要添加? – mymotherland 2012-08-17 09:21:38

+0

拥有证书是一回事,但你需要一些东西来验证它,你可以谷歌卷曲cacert捆绑文件下载 – 2012-08-17 10:06:13

回答

1

我会在命令行上称为

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

,并试图拿起或管道输出回来。也许在php中有一个包装,否则系统命令似乎是最简单的路线。

+0

@eirikima我发现这个http://forum.nusphere.com/converting-cer-to-pem-via-php-script- t8451.html并尝试过,但不能正常工作。 – mymotherland 2012-08-17 14:35:31

0

我在为iOS开发时遇到了同样的问题。我们howerve有* .CER证书,并改变了他们对*。质子交换膜使用以下命令行:
OpenSSL的X​​509 -inform DER -in aps_production_identity.cer退房手续aps_production_identity.pem

如果这不起作用确保包括如下所示的私钥: Apple Push Notification Service