1

Apple プッシュ通知用のサーバー側接続を作成しようとしています。まず、ユーザー (おそらく ios 開発者) に、Apple が .pem ファイルにするために提供する .cer および .p12 ファイルを提供するように依頼します。

以下は、.pem 証明書の作成です。

$dir =  $this->directory.'/certificates';
$password = 'a_user_password';
$certificate = $dir.'/certificate.cer';
$key_password =  $dir.'/key.p12';

exec('openssl x509 -inform der -in '.$certificate.' -out '.$dir.'/certificate.pem');
exec('openssl pkcs12 -nocerts -out '.$dir.'/key.pem -in '.$key_password.' -passout pass:'.$password.' -passin pass:'.$password);

$filename = $key_password;
$results = array();
$worked = openssl_pkcs12_read(file_get_contents($filename), $results, $obj->password);
if($worked) {
   $current = file_get_contents($dir.'/key.pem');
   $current .= $results['pkey'];
   file_put_contents($dir.'/key.pem', $current);
} else {
   echo openssl_error_string();
}
exec('cat '.$dir.'/certificate.pem '.$dir.'/key.pem > '.$dir.'/apns_certificate.pem');

ここまでは順調ですね。上記で生成された apns_certificate.pem が Apple でコマンドラインから成功することを次の方法でテストしました。

s_client -connect gateway.sandbox.push.apple.com:2195 -cert certificate.pem -key key.pem

ただし、PHPを介してapnsに接続しようとするとできません。私が試した最後のphpコードに従いますが、他の人がうまくいったことを見てきました:

$this->certificate = ROOT.'/certificates/apns_certificate.pem';
$this->socket = 'ssl://gateway.push.apple.com:2195';
if (!file_exists($this->certificate)) {
        $this->error = 'Certificate file not found';
        return false;
    }

    $this->stream_context = stream_context_create();
    $this->stream_options = array(
        'ssl' => array(
            'local_cert' => $this->certificate,
            'passphrase' => 'a_user_password', //same with the one used in my previous code
        )
    );
    $success = stream_context_set_option($this->stream_context, $this->stream_options);
    if ($success == false) {
        $this->error = 'Secure connection failed';
        return false;
    }

    $this->socket_client = stream_socket_client($this->socket, $con_error, $con_error_string, $this->timeout, STREAM_CLIENT_CONNECT, $this->stream_context);

    if ($this->socket_client === false) {
        $this->error = $con_error_string;
        return false;
    } else {
        return true;
    }

上記のコードはエラーを返します: 警告: stream_socket_client(): SSL 操作がコード 1 で失敗しました。 //gateway.push.apple.com:2195

よろしくお願いします。

4

1 に答える 1