之前已经问过这个问题,但在答案中,代码不起作用。
如何在不使用端口2195的情况下从PHP发送iOS推送通知,端口2195在我的主机上被阻止(one.com)
我试过这段代码:
$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(连接超时)