【问题标题】:PHP Push Notification for iOS works at command line, but not with Apache适用于 iOS 的 PHP 推送通知适用于命令行,但不适用于 Apache
【发布时间】:2017-02-25 13:22:03
【问题描述】:

您好,我正在发送推送通知,这在命令行中效果很好,但在作为 php 网页运行时效果不佳。

路径是正确的 apn 和密码(完全相同的代码)。

我运行页面,但被 apache 调用时失败。

我收到以下错误/警告:

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in ..pushnotifications.php on line 31

Warning: stream_socket_client(): Failed to enable crypto in ..pushnotifications.php on line 31

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in ..pushnotifications.php on line 31

Failed to connect: 0

代码:

public function iOS($data, $devicetoken, $sandbox) {
    $deviceToken = $devicetoken;

    $ctx = stream_context_create();
    if($sandbox) {
        stream_context_set_option($ctx, 'ssl', 'local_cert',  __DIR__.'/apn/apns-dev-cert.pem');
    } else {
        stream_context_set_option($ctx, 'ssl', 'local_cert',  __DIR__.'/apn/apns-prod-cert.pem');
    }
    stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);
    // Open a connection to the APNS server
    // 'gateway.push.apple.com:2195'; //Production
    // 'gateway.sandbox.push.apple.com:2195'; // Sandbox
    if($sandbox) {
        $fp = stream_socket_client(
            'ssl://gateway.sandbox.push.apple.com:2195', $err,
            $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    } else {
        $fp = stream_socket_client(
            'ssl://gateway.push.apple.com:2195', $err,
            $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    }

    if (!$fp)
        exit("Failed to connect: $err $errstr" . PHP_EOL);
    // Create the payload body
    $body['aps'] = array(
        'alert' => array(
            'title' => $data['title'],
            'body' => $data['body'],
         ),
        'sound' => $data['sound'],
        'badge' => $data['badge']
    );
    // Encode the payload as JSON
    $payload = json_encode($body);
    // Build the binary notification
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
    // Send it to the server
    $result = fwrite($fp, $msg, strlen($msg));

    // Close the connection to the server
    fclose($fp);
    if (!$result)
        return 'Message not delivered' . PHP_EOL;
    else
        return 'Message successfully delivered' . PHP_EOL;
}

【问题讨论】:

  • 你能在第31行显示代码吗,因为它提到了第31行
  • 你能确认一下.pem文件的路径吗,
  • $sandbox = true; $msg_payload = array ( 'title' => 'Test', 'body' => '你收到了一个新订单', 'sound' => 'default', 'badge' => 3 ); $deviceToken = '994A7663BCA0D0EC75BC6923D27741CC1D2C1E089995EF0BF632185CAAFDCA35'; echo PushNotifications::iOS($msg_payload, $deviceToken, $sandbox);
  • 这是第 31 行: $fp = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx );
  • 检查 .pem 文件的路径,或检查此应用的配置文件是否过期

标签: php ios push-notification pem apn


【解决方案1】:

在经历了很多挫折之后,路径才是问题所在。我不想将 pem 文件放在与代码相同的路径中,但这是唯一可行的方法!

如果我输入不同的路径,命令行可以工作,但 Apache 没有,如果它与代码在同一个文件夹中,两者都可以工作。

希望对其他人有所帮助。

感谢 Fahad Jamal 为我指明了正确的方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2013-03-28
    • 1970-01-01
    • 2011-03-28
    相关资源
    最近更新 更多