【发布时间】:2014-11-09 14:00:37
【问题描述】:
这个问题之前有人问过,但是在回答中,代码不起作用。
如何在不使用我的主机 (one.com) 上被阻止的端口 2195 的情况下从 PHP 发送 iOS 推送通知
我试过这段代码:
$url = 'https://gateway.sandbox.push.apple.com:2195';
$cert = 'ck.pem';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'password_hidden_on_stack_overflow');
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"device_tokens": ["458e5939b2xxxxxxxxxxx3"], "aps": {"alert": "test message one!"}}');
$curl_scraped_page = curl_exec($ch);
但它给了我这个错误:解析错误:语法错误,意外的'*'
如何解决?
编辑
我也试过这个代码:
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'password_hidden_on_stack_overflow'); // Add your own ck.pem passphrase here
foreach($deviceIDs as $deviceToken) {
// 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' => $message,
'sound' => 'default'
);
$payload = json_encode($body);
// Build & send notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
/*if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;*/
}
// Close the connection to the server
fclose($fp);
但它给了我这个错误:警告:stream_socket_client():无法连接到 ssl://gateway.sandbox.push.apple.com:2195(连接超时)
【问题讨论】:
标签: php ios notifications push