{"id":13390,"date":"2024-09-02T15:43:01","date_gmt":"2024-09-02T07:43:01","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=13390"},"modified":"2024-09-02T15:43:01","modified_gmt":"2024-09-02T07:43:01","slug":"php-%e4%bd%bf%e7%94%a8-php-5-4-%e6%8e%a8%e6%92%ad-firebase-cloud-messaging-api-v1","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=13390","title":{"rendered":"PHP - \u4f7f\u7528 PHP 5.4 \u63a8\u64ad Firebase Cloud Messaging API (V1)"},"content":{"rendered":"<p>--<\/p>\n<h2>\u524d\u8a00<\/h2>\n<ul>\n<li><a href=\"https:\/\/firebase.google.com\/docs\/cloud-messaging\/migrate-v1?hl=zh-tw\" target=\"_blank\" rel=\"noopener\">\u5f9e\u820a\u7248 FCM API \u9077\u79fb\u81f3 HTTP v1 \u00a0|\u00a0 Firebase Cloud Messaging (google.com)<\/a><\/li>\n<\/ul>\n<p>\u6700\u8fd1 (2024\/8) \u5ba2\u6236\u53cd\u61c9\u6709\u6389\u63a8\u64ad\u554f\u984c\uff0c\u76f4\u5230\u53bb\u67e5\u770b\u63a8\u64ad\u7d50\u679c\u770b\u5230 {\"error\":\"Deprecated endpoint, see https:\/\/firebase.google.com\/docs\/cloud-messaging\/migrate-v1\"} \uff0c\u624d\u767c\u73fe\u820a\u7248 FCM API \u5df2\u7d93\u5728 2024\/7\/24 \u505c\u6b62\u670d\u52d9\uff0c\u5fc5\u9808\u8981\u66f4\u65b0\u5230 HTTP v1 API<\/p>\n<p>--<\/p>\n<h2>\u53c3\u8003\u8cc7\u6e90<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.cnblogs.com\/aiyinsitan\/p\/16968237.html\">PHP Firebase HTTP v1 API \u65b0\u7248\u63a8\u9001 - \u7231\u5624\u65af\u5766i - \u535a\u5ba2\u56ed (cnblogs.com)<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/tz0922\/article\/details\/135359630\" target=\"_blank\" rel=\"noopener\">php - \u901a\u8fc7 FCM google-php-api HTTPv1 API \u53d1\u9001\u63a8\u9001\u6d88\u606f_fcm apis-CSDN\u535a\u5ba2<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<h2>\u4e0b\u8f09 Google API<\/h2>\n<p>\u56e0\u70ba PHP \u7248\u672c\u592a\u820a\uff0c\u6240\u4ee5\u7121\u6cd5\u4f7f\u7528 composer \u4f86\u53d6\u5f97 Google API\uff0c\u53ea\u80fd\u5f9e GitHub \u4f86\u4e0b\u8f09\u3002<\/p>\n<p>\u9019\u88e1\u4e0b\u8f09\u7684\u7248\u672c\u662f <a href=\"https:\/\/github.com\/googleapis\/google-api-php-client\/releases\/download\/v2.8.0\/google-api-php-client-v2.8.0-PHP5.4.zip\" target=\"_blank\" rel=\"noopener\">v2.8.0<\/a>\uff0cv2.9.0 \u4ee5\u4e0a\u5c31\u4e0d\u652f\u63f4 5.4 &amp; 5.5 \u4e86<\/p>\n<p>--<\/p>\n<h2>\u5b8c\u6574\u7a0b\u5f0f\u78bc<\/h2>\n<p>\u74b0\u5883\uff1aPHP 5.4<\/p>\n<pre class=\"lang:php decode:true\">&lt;?php\r\nrequire_once __DIR__ .'\/..\/vendor\/google-api-php-client-v2.8.0-PHP5.4\/vendor\/autoload.php';\r\n\r\nuse Google\\Client;\r\n$client = new Google\\Client();\r\n$client-&gt;useApplicationDefaultCredentials();\r\n$client-&gt;setAuthConfig( __DIR__ .'\/..\/vendor\/google-api-php-client-v2.8.0-PHP5.4\/upad12-xxxxxx.json');\/\/\u5f15\u5165json\u79d8\u94a5\r\n$client-&gt;setScopes('https:\/\/www.googleapis.com\/auth\/firebase.messaging'); \/\/ \u6388\u4e88\u8bbf\u95ee FCM \u7684\u6743\u9650\r\n\r\n$send_url     = \"https:\/\/fcm.googleapis.com\/v1\/projects\/upad12-4c8c9\/messages:send\";\r\n$access_token = $client-&gt;fetchAccessTokenWithAssertion();\/\/\u83b7\u53d6\u79d8\u94a5\r\n\r\nif (!isset($access_token['access_token'])) {\r\n    return json_encode(['err_no' =&gt; 1, 'msg' =&gt; '\u63a8\u9001\u5931\u8d25\uff0c\u672a\u83b7\u53d6\u5230\u79d8\u94a5'],JSON_UNESCAPED_UNICODE);\r\n}\r\n$accessToken = $access_token['access_token'];\/\/\u79d8\u94a5\r\n\r\n$params = [\r\n    \"message\" =&gt; [\r\n        \"token\"        =&gt;'\u8a2d\u5099 token',\r\n        \"notification\" =&gt; [\r\n            \"title\"    =&gt; 'title',\r\n            \"body\"     =&gt; 'body'\r\n        ]\r\n    ]\r\n];\r\n\r\n\/\/header\u8bf7\u6c42\u5934\uff0c$accessToken \u5c31\u662f\u4f60\u4e0a\u9762\u83b7\u53d6\u7684\u4ee4\u724c\r\n$header = [\r\n    'Content-Type: application\/json',\r\n    'Authorization: Bearer ' . $accessToken,\r\n];\r\n$params = json_encode($params);\r\n$response = http_post($send_url, $params, $header);\r\n$response = json_decode($response,true);\r\nif(isset($response['name'])){\r\n    echo  json_encode(['err_no' =&gt; 0, 'msg' =&gt; '\u63a8\u9001\u6210\u529f',['data'=&gt;$response]],JSON_UNESCAPED_UNICODE);\r\n    exit();\r\n}elseif(isset($response['error'])){\r\n    echo  json_encode(['err_no' =&gt; 1, 'msg' =&gt; '\u6d88\u606f\u672a\u9001\u8fbe',['data'=&gt;$response]],JSON_UNESCAPED_UNICODE);\r\n    exit();\r\n}\r\n\r\nfunction http_post($send_url,$params,$headers){\r\n    $curl = curl_init();\r\n\r\n    curl_setopt_array($curl, array(\r\n        CURLOPT_URL =&gt; $send_url,\r\n        CURLOPT_RETURNTRANSFER =&gt; true,\r\n        CURLOPT_ENCODING =&gt; '',\r\n        CURLOPT_MAXREDIRS =&gt; 10,\r\n        CURLOPT_TIMEOUT =&gt; 0,\r\n        CURLOPT_FOLLOWLOCATION =&gt; true,\r\n        CURLOPT_HTTP_VERSION =&gt; CURL_HTTP_VERSION_1_1,\r\n        CURLOPT_CUSTOMREQUEST =&gt; 'POST',\r\n        CURLOPT_POSTFIELDS =&gt; $params,\r\n        CURLOPT_HTTPHEADER =&gt; $headers\r\n    ));\r\n\r\n    $response = curl_exec($curl);\r\n    curl_close($curl);\r\n    return $response;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<h2>\u5176\u4ed6\u53c3\u8003\u8cc7\u6e90<\/h2>\n<ul>\n<li><a href=\"https:\/\/github.com\/kedniko\/firebase-cloud-messaging-http-v1-php?tab=readme-ov-file\" target=\"_blank\" rel=\"noopener\">GitHub - kedniko\/firebase-cloud-messaging-http-v1-php<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/49782134\/how-to-use-the-fcm-http-v1-api-with-php\" target=\"_blank\" rel=\"noopener\">firebase - How to use the FCM HTTP v1 API with php - Stack Overflow<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"13390\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,223&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u524d\u8a00 \u5f9e\u820a\u7248 FCM A...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"13390\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,223&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[260],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/13390"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13390"}],"version-history":[{"count":5,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/13390\/revisions"}],"predecessor-version":[{"id":13395,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/13390\/revisions\/13395"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}