> For the complete documentation index, see [llms.txt](https://tobyisme.gitbook.io/php/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tobyisme.gitbook.io/php/li-yong-curl-tui-boios-ff0c-yong-ios-de-token.md).

# 利用curl推播ios，用ios的token

```php
$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+

參考網址：

* [基于HTTP/2与Token的APNs新协议](http://www.skyfox.org/apple-push-with-auth-key-token.html)
* [HTTP/2 Response from APNs](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1)
* [token-based authentication APNS 推播範例 使用C#](https://tpu.thinkpower.com.tw/tpu/File/html/201611/20161122001842_f.html?f=3dj6j8kd38895ksgtdddd93865jhr9sn3rqkh)
