【发布时间】:2020-02-09 08:27:32
【问题描述】:
当我在本地机器 (MAMP PRO 5.4) 上测试以下代码时,我可以在我的设备上收到推送通知。
function sendTestMessage(){
$keyfile = 'MyAuthKey.p8';
$keyid = 'MyKeyId';
$teamid = 'MyTeamId';
$bundleid = 'myBundleId';
$url = 'https://api.development.push.apple.com';
$token = 'device token';
$message = '{"aps":{"alert":"Hi there!","sound":"default"}}';
$key = openssl_pkey_get_private('file://'.$keyfile);
$header = ['alg'=>'ES256','kid'=>$keyid];
$claims = ['iss'=>$teamid,'iat'=>time()];
$header_encoded = $this->base64($header);
$claims_encoded = $this->base64($claims);
$signature = '';
openssl_sign($header_encoded . '.' . $claims_encoded, $signature, $key, 'sha256');
$jwt = $header_encoded . '.' . $claims_encoded . '.' . base64_encode($signature);
// only needed for PHP prior to 5.5.24
if (!defined('CURL_HTTP_VERSION_2_0')) {
define('CURL_HTTP_VERSION_2_0', 3);
}
$http2ch = curl_init();
curl_setopt_array($http2ch, array(
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
CURLOPT_URL => "$url/3/device/$token",
CURLOPT_PORT => 443,
CURLOPT_HTTPHEADER => array(
"apns-topic: {$bundleid}",
"authorization: bearer $jwt"
),
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $message,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => 1
));
$result = curl_exec($http2ch);
if ($result === FALSE) {
throw new Exception("Curl failed: ".curl_error($http2ch));
}
$status = curl_getinfo($http2ch, CURLINFO_HTTP_CODE);
echo $status;
}
但是,当我在我的服务器(AWS EC2 Ubuntu 16.04)上测试它时,我收到以下错误:
b>Fatal error</b>: Uncaught Exception: Curl failed: No URL set! in /var/www/html/classes/PushNotification.php:171
Stack trace:
#0 /var/www/html/api/v1/testPush.php(12): PushNotification->sendTestMessage()
#1 {main}
thrown in <b>/var/www/html/classes/PushNotification.php</b> on line <b>171</b><br />
我已经在这里检查并尝试了大多数类似的解决方案,但它不起作用。
【问题讨论】:
标签: php ios curl apple-push-notifications php-curl