2017-02-25 5 views
1

Hallo Ich sende Push-Benachrichtigung, die funktioniert großartig an der Befehlszeile, aber nicht, wenn es als PHP-Webseite ausgeführt wird.PHP Push Notification für iOS funktioniert auf der Kommandozeile, aber nicht mit Apache

Die Pfade sind korrekt Apn und und Passphrase (genau gleichen Code).

Ich führe die Seite, aber schlägt fehl, wenn sie von Apache aufgerufen wird.

ich diese Fehler/Warnungen:

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 

Code:

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; 
} 
+0

können Sie den Code in der Zeile 31 anzeigen, weil es die Zeile 31 –

+0

enthält, können Sie den Pfad der .pem-Dateien bestätigen, –

+0

$ sandbox = true; \t $ msg_payload = array ( \t \t 'title' => 'Test', \t \t 'body' => 'Sie haben einen neuen Auftrag erhalten', \t \t 'Sound' => 'default', \t \t "Abzeichen" => 3 \t); \t \t \t $ deviceToken = '994A7663BCA0D0EC75BC6923D27741CC1D2C1E089995EF0BF632185CAAFDCA35'; \t Echo PushNotifications :: iOS ($ msg_payload, $ deviceToken, $ Sandbox); –

Antwort

0

Nach viel Frustration der Weg das Problem war. Ich wollte die PEM-Datei nicht auf den gleichen Pfad wie Code stellen, aber das war die einzige Möglichkeit!

Befehlszeile funktionierte, wenn ich einen anderen Pfad eingab, aber Apache nicht, beide funktionierten, wenn es im selben Ordner wie Code war.

Ich hoffe, es hilft anderen.

Danke Fahad Jamal dafür, dass er mich in die richtige Richtung zeigt.

Verwandte Themen