我的推送通知遇到了一些问题。我已经阅读了大量的教程,并提出了下面的脚本。我似乎无法连接到APN。只是为了澄清,我没有编写应用程序的应用程序端代码。我只懂PHP和其他基于Web的语言。
我已经完成了从证书和私钥创建.pem文件的整个过程。我的网络服务器也有SSL证书(我是通过我的网络主机提供商做的,不确定这是否是正确的方法)。我只是不确定为了测试一切都运行正常而发生了什么。
当我在网页上加载脚本时,我收到错误:警告:stream_socket_client()[function.stream-socket-client]:无法连接到ssl://gateway.sandbox.push.apple.com: 2195(连接超时)连接失败:110连接超时
您能否仔细查看代码并帮助我找到无法连接到APN的原因。还有一种方法可以获得更详细的错误报告吗?
这是我的代码:(问题应该在前15行左右。)
<?PHP
$username = $_GET['username'];
$userIDarr = $_GET['userARR'];
$message = $username.' is d2P';
$passphrase = 'mypass'; // not sure what this refers too.. (from what i have read I think it is meant to be for the iphone side of things)
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'ck.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
// Open a connection to the APNS server
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 10, STREAM_CLIENT_CONNECT, $streamContext);
if (!$apns)
exit("Failed to connect: $error $errorString" . PHP_EOL);
echo 'Connected to Apple service. ' . PHP_EOL;
@mysql_connect ("localhost","down2par_down2pa","4m329aMh") or die ("could not connect to MySQL");
@mysql_select_db ("down2par_d2pdb") or die ("no database");
// create array of all device IDs AND badges that will be receiving notifications
$SQLarr = implode(" AND userid =", $userIDarr);
$DB = mysql_query("SELECT diviceID, badge FROM new_fb_users WHERE userid = '$SQLarr'");
while($DBarr = mysql_fetch_array($DB)) {
$deviceToken = $DBarr['deviceID'];
$badge = $DBarr['badge'];
$id = $DBarr['id'];
mysql_query("UPDATE new_fb_users SET badge = badge+1 WHERE id = $id");
// create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => ($badge > 0 ? $badge + 1 : 1)
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0).pack('n', 32).pack('H*', str_replace(' ', '', $deviceToken)).pack('n', strlen($payload)).$payload;
// Send it to the server
$result = fwrite($apns, $msg, strlen($msg));
if (!$result)
echo 'Failed message'.PHP_EOL;
else
echo 'Successful message'.PHP_EOL;
}
// Close the connection to the server
fclose($apns);
?>