利用curl推播ios,用ios的token

$keyfile = 'D:\Projects-VS2017\TokenbasedAPNsSample-master\TokenbasedAPNsSample\bin\Debug\AuthKey_PEVA6L37A6.p8';               # <- Your AuthKey file
$keyid = 'Key ID';                    # <- Your Key ID
$teamid = 'Team ID';                  # <- Your Team ID (see Developer Portal)
$bundleid = 'Bundle ID';              # <- Your Bundle ID
$url = 'https://api.push.apple.com';  # <- development url, or use http://api.push.apple.com for production environment
$token = 'Device 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 = rtrim(strtr(base64_encode(json_encode($header)), '+/', '-_'), '=');
$claims_encoded = rtrim(strtr(base64_encode(json_encode($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
));
curl_setopt($http2ch, CURLOPT_URL, "$url/3/device/$token");
$result = curl_exec($http2ch);

if ($result === FALSE) {
    echo("Curl failed: " . curl_error($http2ch));
}

$status = curl_getinfo($http2ch, CURLINFO_HTTP_CODE);

openssl >= 1.0.2e

PHP >= 5.5.24 curl 7.46+

參考網址:

Last updated