1

ターミナルで以下のスクリプトを実行すると、エラーが発生します:

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
/Applications/MAMP/htdocs/SimplePush/simplepush.php on line 21

Warning: stream_socket_client(): Failed to enable crypto in /Applications/MAMP/htdocs/SimplePush/
simplepush.php on line 21

Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195
(Unknown error) in /Applications/MAMP/htdocs/SimplePush/simplepush.php on line 21
Failed to connect: 0 

コードは次のとおりです。

<?php

// Put your device token here (without spaces):
$deviceToken = '*******';

// Put your private key's passphrase here:
$passphrase = '***';

// Put your alert message here:
$message = 'Want more credits!';


$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$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);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);

// 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));

if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

?>
4

4 に答える 4

1

開発証明書を使用しているように聞こえるので、次を指してみてください gateway.sandbox.push.apple.com:2195。URLgateway.push.apple.com:2195は本番証明書用です。

于 2013-01-09T12:38:56.033 に答える
0

私はあなたのスクリプトをそのように使用しましたが、私が見つけた解決策は、phpスクリプト内でCURL呼び出しから呼び出すことでした:

これは私のために働くコードです。HTTPS経由で呼び出すことを忘れないでください

スクリプト 1 (CURL 経由で呼び出す):

// set post fields
    $post = [
        'mensaje' => 'THE MESSAGE TO SEND',
        'device_id' => $device_id,
    ];

    $ch = curl_init('https://www.DOMAIN/push-curl-script.php');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($ch);
    curl_close($ch);

プッシュ-curl-script.php:

    $certificado_push   = "*****.pem";

    // Put your device token here (without spaces):
    $deviceToken        = $_POST["device_id"];

    // Put your private key's passphrase here:
    $passphrase         = '***';

    // Put your alert message here:
    $message            = $_POST["mensaje"];


    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', $certificado_push);
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

    // Open a connection to the APNS server
    $fp = stream_socket_client(
    'ssl://gateway.'.($sandbox ? 'sandbox.' : '').'push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$fp) {
            echo "Failed to connect: $err $errstr" ;
    }else{    
            echo 'Connected to APNS'  ;

            // Create the payload body
            $body['aps'] = array(
            'alert' => $message,
            'sound' => 'default'
            );

            // 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));

            if (!$result) {
                echo 'Message not delivered' ;
            }else{
                echo 'Message successfully delivered' ;
            }

            // Close the connection to the server
            fclose($fp);
     }       
于 2017-02-24T21:11:25.687 に答える