2013-10-20 152 views
0

您好,我嘗試通過FTP通過FTP-Hoster square7.ch發送推送通知,但我總是會遇到一些錯誤。證書在服務器上。 任何人都可以幫助我嗎?iOS推送通知PHP錯誤

這裏是PHP代碼:

<?php 
$streamContext= stream_context_create(); 
stream_context_set_option($streamContext , 'ssl','local_cert' , 'TestPushApp.pem'); 
//stream_context_set_option($streamContext , 'ssl' , 'passphrase','password'); 
$socketClient = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$error,$errorString,60,STREAM_CLIENT_CONNECT,$streamContext); 
$payload['aps']= array('alert' => 'Erste Push Nachricht ueber PHP','sound' => 'default','badge' => '20'); 
$payload= json_encode($payload); 
echo $payload; 
$deviceToken = str_replace(' ','','XXXXXXXXXXXXXXXXXX'); 
$message= pack('CnH*',0,32,$devicetoken); 
$message= $message . pack ('n',strlen($payload)); 
$message= $messgae . $payload; 
fwrite($socketClient,$message); 
fclose($socketClient); 

?> 

這裏的錯誤消息:

Warning: stream_socket_client() [function.stream-socket-client]: SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in /users/michaellll/www/push.php on line 5 

Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /users/michaellll/www/push.php on line 5 

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /users/michaellll/www/push.php on line 5 
{"aps":{"alert":"Erste Push Nachricht ueber PHP","sound":"default","badge":"20"}} 
Warning: fwrite() expects parameter 1 to be resource, boolean given in /users/michaellll/www/push.php on line 13 

Warning: fclose() expects parameter 1 to be resource, boolean given in /users/michaellll/www/push.php on line 14 
+0

請聯繫您的託管服務提供商,請確認2195或2196端口是否打開。 – Abhijit

+0

如何檢查端口是否打開? –

+0

請聯繫您的託管服務提供商 – Abhijit

回答

3
// Push Notification code for IPHONE in PHP 
    $deviceToken = $users_rows['gcm_regid']; 
    $passphrase = 'pass1234'; 
    $ctx = stream_context_create(); 
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'DrinksterDevelopment.pem'); 
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

    $fp = stream_socket_client(
     'ssl://gateway.sandbox.push.apple.com:2195', $err, 
     $errstr, 120, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

    if (!$fp) 
     exit("Failed to connect: $err $errstr" . PHP_EOL); 

    echo 'Connected to APNS' . PHP_EOL; 

    $body['aps'] = array(
     // 'alert' => $_GET["message"].'#'.$_GET["type"].'#'.$_GET["deal_id"], 
     'alert' => $_GET["message"], 
     'sound' => 'default' 
     ); 
    $body['other'] = $_GET["type"].'#'.$_GET["deal_id"]; 

    $payload = json_encode($body); 
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 
    $result_iphone = fwrite($fp, $msg, strlen($msg)); 

    if (!$result_iphone) 
     $msg_iphone = 'Message not delivered' . PHP_EOL; 

    else 
     $msg_iphone = 'Message successfully delivered' . PHP_EOL; 

    mail('[email protected]', 'IOSPushMsgStatus', $msg_iphone); 
    fclose($fp); 
    } 
+0

我嘗試了代碼,並得到相同的錯誤。它似乎是代碼是不是問題 –

+0

我現在在其他兩臺服務器上試過它,它每次都會失敗,你的代碼說:(無法連接:)它是否在你的服務器上工作 –

0

我認爲它的SSL問題,請啓用SSL那麼它可能會奏效。還有一兩件事是有stream_context_set_option($streamContext , 'ssl','local_cert', 'TestPushApp.pem'); 它必須是這樣的

$options = array('ssl' => array('local_cert' => 'include/TestPushApp.pem','passphrase' => '//Provided passpharase to you')); 
stream_context_set_option($streamContext , $options); 

,如果您有生產許可證我。 e pem file then then use of ssl://gateway.push.apple.com:2195

這裏文件寫入功能只包含兩個參數fwrite($fp, $msg);不是那個打包消息的長度。