{"id":250,"date":"2023-10-20T13:22:55","date_gmt":"2023-10-20T05:22:55","guid":{"rendered":"https:\/\/www.nickyoo7.com\/?p=250"},"modified":"2023-10-20T13:22:55","modified_gmt":"2023-10-20T05:22:55","slug":"ios-%e5%bb%ba%e7%ab%8b%e9%81%a0%e8%b7%9d%e6%8e%a8%e6%92%ad-app-%e4%ba%94-%e6%8e%a8%e6%92%ad%e6%b8%ac%e8%a9%a6-%e4%bd%bf%e7%94%a8-php","status":"publish","type":"post","link":"https:\/\/www.nickyoo7.com\/?p=250","title":{"rendered":"iOS \u5efa\u7acb\u9060\u8ddd\u63a8\u64ad App (\u4e94) \u63a8\u64ad\u6e2c\u8a66 \u2013 \u4f7f\u7528 php"},"content":{"rendered":"<p>iOS \u5efa\u7acb\u9060\u8ddd\u63a8\u64ad App (\u4e94) \u63a8\u64ad\u6e2c\u8a66 \u2013 \u4f7f\u7528 php<br \/>\n<!--more-->1.\u4f3a\u670d\u5668\u6aa2\u67e5\u4e8b\u9805<br \/>\n(1) Web Server \u9700\u652f\u63f4 HTTP\/2<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\u6aa2\u6e2c\u65b9\u6cd5\u4e00 : For Linux\r\n$ curl --http2 -I https:\/\/www.google.com\r\n\u4e0d\u652f\u63f4\u51fa\u73fe :\r\ncurl : (1) Unsupported protocol\r\n\r\n\u6aa2\u6e2c\u65b9\u5f0f\u4e8c : \u4f7f\u7528 php\r\n&lt;?php\r\n    echo $_SERVER[ \"SERVER_PROTOCOL\" ];\r\n<\/pre>\n<p>(2) PHP \u7248\u672c\u9700 5.5.24 \u7248\u4ee5\u4e0a php \u7684 curl \u624d\u6709\u652f\u63f4 HTTP\/2<\/p>\n<p>2. PHP \u7a0b\u5f0f\u78bc<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\n    $team_id=\"\"; \/\/\u958b\u767c\u8005 team ID\r\n    $key_id=\"\";  \/\/p8 \u683c\u5f0f\u8b49\u66f8\u7684 ID\r\n    $pkey_file=\"AuthKey_xxxxxx.p8\"; \/\/p8\u8b49\u66f8\u7684\u5b58\u653e\u4f4d\u5740\uff0c\u52ff\u4f7f\u7528\u4e2d\u6587\u3002\u7d55\u5c0d\u8def\u5f91\r\n    $bundle_id=\"\"; \/\/App\u7684 Boundle ID\r\n    $device_token=\"\"; \/\/Device token\r\n    \r\n    $msg=\"\u6e2c\u8a66\u8a0a\u606f\";\r\n    $r=Sent_Notification_iOS($team_id ,$key_id,$pkey_file,$bundle_id,$device_token,$msg);\r\n    \r\nfunction Sent_Notification_iOS($team_id ,$key_id,$pkey_file,$bundle_id,$device_token,$msg){\r\n    \/\/ Create the notification payload\r\n    $payload = array(\r\n        'aps' =&gt; array(\r\n            'alert' =&gt; $msg,\r\n            'sound' =&gt; 'default'\r\n        )\r\n    );\r\n    $payload_json = json_encode($payload);\r\n\r\n    \/\/\u6b63\u5f0f\u74b0\u5883\u63a8\u9001\r\n    \/\/$url = 'https:\/\/api.push.apple.com\/3\/device\/' . $device_token;\r\n\r\n    \/\/\u6c99\u76d2\u63a8\u9001\u5730\u5740\r\n    $url = \"https:\/\/api.sandbox.push.apple.com\/3\/device\/\".$device_token;\r\n    \r\n    $jwt = get_JWT($team_id ,$key_id,$pkey_file,$bundle_id);\r\n        \r\n    $headers = array(\r\n        'Authorization: Bearer '.$jwt,\r\n        'Content-Type: application\/json',\r\n       \t'apns-topic: '.$bundle_id,\r\n    );\r\n    $ch = curl_init($url);\r\n    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload_json);\r\n    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); \/\/\u9019\u91cc\u5f88\u95dc\u9375\uff0c\u4ee5http2\u767c\u9001\u8acb\u6c42\uff0c\u5982\u679c\u4f60\u7684curl\u4e0d\u652f\u6301http2\u5247\u9700\u8981\u770b\u524d\u9762\u7684\u6b65\u9a5f\r\n    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n    $response = curl_exec($ch);\r\n    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);\r\n    var_dump($response);\r\n    curl_close($ch);\r\n\r\n    \/\/ Check the response\r\n    if ($http_code == 200) {\r\n        echo 'Notification sent successfully.';\r\n    } else {\r\n        echo 'Notification failed with error code: ' . $http_code;\r\n    }\r\n    \r\n}\r\nfunction get_JWT($team_id ,$key_id,$pkey_file,$bundle_id){\r\n\r\n\/\/ token\u904e\u671f\u6642\u9593\r\n    $expires = time() + 3600;\r\n\r\n    \/\/ Generate the JWT header\r\n    $header = array(\r\n        'alg' =&gt; 'ES256',\r\n        'kid' =&gt; $key_id\r\n    );\r\n\r\n    $jwt_header = base64_encode(json_encode($header));\r\n\r\n    \/\/ Generate the JWT payload\r\n    $payload = array(\r\n        'iss' =&gt; $team_id,\r\n        'iat' =&gt; time()\r\n    );\r\n    $jwt_payload = base64_encode(json_encode($payload));\r\n\r\n    \/\/\u8f09\u5165\u8b49\u66f8\u6587\u4ef6\r\n    $pkey_contents = file_get_contents($pkey_file);\r\n    $pkey = openssl_get_privatekey($pkey_contents);\r\n\r\n    \/\/JWT \u7c3d\u540d\r\n    $signature = '';\r\n    openssl_sign($jwt_header . '.' . $jwt_payload, $signature, $pkey, 'sha256');\r\n    $jwt_signature = base64_encode($signature);\r\n    \r\n    \/\/ Generate the final JWT\r\n    $jwt = $jwt_header . '.' . $jwt_payload . '.' . $jwt_signature;\r\n    return $jwt;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>iOS \u5efa\u7acb\u9060\u8ddd\u63a8\u64ad App (\u4e94) \u63a8\u64ad\u6e2c\u8a66 \u2013 \u4f7f\u7528 php<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,6],"tags":[],"class_list":["post-250","post","type-post","status-publish","format-standard","hentry","category-ios","category-php","entry"],"_links":{"self":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=250"}],"version-history":[{"count":1,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":251,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions\/251"}],"wp:attachment":[{"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nickyoo7.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}