2011-09-14 167 views
0

我需要用php服務器實現蘋果推送通知。我認爲連接正常工作,但由於某種原因蘋果服務器拒絕該信息。蘋果推送通知..拒絕?

我得到這個錯誤:

"error 0errorString
\nFatal error: Call to undefined function socket_close() in /home/www/76cdd1fbdfc5aca0c9f07f489ecd9188/web/mobileApp/handleRequest.php on line 420
\n"

這是我簡單的代碼(用假消息,只是爲了測試):

$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => unread, 'sound' => 'default'); 
    $payload['server'] = array('serverId' => $serverId, 'name' => $name); 
    $payload = json_encode($payload); 

    $apnsHost = 'gateway.sandbox.push.apple.com'; 
    $apnsPort = 2195; 
    $apnsCert = 'ck.pem'; 

    $streamContext = stream_context_create(); 
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert); 

    $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext); 

    echo "error " . $error; 
    echo "errorString" . $errorString; 

    $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload; 
    fwrite($apns, $apnsMessage); 

    socket_close($apns); 
    fclose($apns); 


    echo "Sent"; 

感謝

+0

反正你應該使用'stream_socket_shutdown()'。 socket_close用於你通過socket_open/socket_accept打開的套接字,你還沒有完成。 –

回答

0

我已經解決了。問題是消息的內容(空白)。套接字函數完美工作。

1

我猜您的PHP版本可能沒有安裝或啓用套接字功能,這就是您遇到此錯誤的原因。

PHP.net摘自:

The socket functions described here are part of an extension to PHP which must be enabled at compile time by giving the --enable-sockets option to configure.

但對你的代碼,你不需要使用socket_close($apns)功能。相反,你可以將其刪除,並使用自接近(Quote from PHP.net):

On success a stream resource is returned which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()), FALSE on failure.

因此,使用fclose($apns);將關閉流

0

替換:

socket_close($apns); 

fclose($apns); 

這應該可以解決你所報告的錯誤:不知道這是否能真正解決APNS問題。

0
$apnsHost = 'gateway.sandbox.push.apple.com'; 

刪除 「沙箱」 從這個URL,它將會運行。