无法连接到APNS(连接超时)

时间:2016-02-08 15:06:13

标签: php ios ssl apple-push-notifications

我之前成功地使用raywenderlich的优秀教程http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1向我的iPhone发送推送通知并使用mac终端运行php脚本,但是,现在我正在努力实现相同的目标只有现在我将push.php脚本上传到我的服务器并通过Cron Job每5分钟运行一次脚本,但我现在收到此错误:

stream_socket_client():无法连接到ssl://gateway.sandbox.push.apple.com:2195(连接超时)

就像我之前说的那样,当我通过终端运行脚本时,它工作得很好。我认为问题是来自服务器的php脚本无法访问我的推送证书。如何让脚本访问它们?请帮忙!

如果有帮助的话,可以使用php脚本:

<?php


// Assign data into variables
$deviceToken = "e39ffc6b98f649f127d07d2d881bc9faa621a3c5d59f9647e64f6452fc37af6c";
$passphrase = "9q3n6k80";
$sound = '';
$blank = "";

$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.sandbox.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' => "mic check",
    '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 succesfully delivered' . PHP_EOL;

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

谢谢!

  

编辑:我正在使用Bluehost中的文件管理器上传文件

0 个答案:

没有答案