2011-07-12 35 views
3

我試圖觸發一個錯誤,所以我可以建立在APNS的錯誤日誌記錄。因此我發送一個太大的有效載荷到服務器。但我沒有錯誤。從PHP蘋果增強推送通知讀取錯誤

代碼:

連接:

$streamContext = stream_context_create(); 
stream_context_set_option($streamContext, 'ssl', 'local_cert', $this->_sslPem); 
stream_context_set_option($streamContext, 'ssl', 'passphrase', ""); 

$this->_apnsConnection = stream_socket_client("ssl://" . Config::$apns['host'] . ":" . Config::$apns['port'], $error, $errorString, $timeout, STREAM_CLIENT_CONNECT, $streamContext); 

if ($this->_apnsConnection) { 
    stream_set_blocking($this->_apnsConnection, 0); 
} 

發送通知:

$this->_log("Sending notification to device token '$deviceToken'"); 

$identifiers = array(); 
for ($i = 0; $i < 4; $i++) { 
    $identifiers[$i] = rand(1, 100); 
} 

$apnsMessage = chr(1) . chr($identifiers[0]) . chr($identifiers[1]) . chr($identifiers[2]) . chr($identifiers[3]) . pack('N', time() + 3600) 
    . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($message)) . $message; 
$bytes = fwrite($this->_apnsConnection, $apnsMessage); 

$this->_log("bytes written: $bytes"); 

$this->_log("Fetching response"); 
$response = fread($this->_apnsConnection, 6); 

$this->_log("Strlen: " . strlen($response)); 

if ($response === false || strlen($response) != 6) { 

    $this->_log("No response..."); 
} else { 

    $responseArray = unpack('Ccommand/CstatusCode/Nidentifier', $response); 
    $this->_log("Response!"); 
    $this->_log($responseArray); 
} 

輸出:

2011-07-12 16:25:55: Sending notification to device token 'XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX' 
2011-07-12 16:25:55: bytes written: 438 
2011-07-12 16:25:55: Fetching response 
2011-07-12 16:25:55: Strlen: 0 
2011-07-12 16:25:55: No response... 

回答

3

我得到了一個解決方案。

解決方案:

$read = array($this->_apnsConnection); 
$null = null; 
$changedStreams = stream_select($read, $null, $null, 0, 1000000); 

if ($changedStreams === false) {  

    $this->_log("Error: Unabled to wait for a stream availability"); 
} elseif ($changedStreams > 0) { 

    $responseBinary = fread($this->_apnsConnection, 6); 
    if ($responseBinary !== false || strlen($responseBinary) == 6) { 

     $response = unpack('Ccommand/Cstatus_code/Nidentifier', $responseBinary); 
     $this->_log($response); 
    } 
} 
+0

由於stream_set_blocking的($這 - > _ apnsConnection,0); fread會在錯誤發生之前立即返回,請閱讀http://stackoverflow.com/questions/4034926/iphone-push-notification-error-response-problem – iTarek