【问题标题】:How to get Webpush subscriberID from console.log into php如何从 console.log 获取 Webpush 订阅者 ID 到 php
【发布时间】:2021-02-26 20:22:37
【问题描述】:

我正在努力将subscribedid 从console.log() 获取到PHP 变量中。 Webpush 已经给出了 js 代码,但不幸的是,当被要求提供帮助时,他们说他们不能。 以下是他们分享的内容。请有人帮助我。

主要的 javascript sn-p(完美运行)。

<!-- start webpushr tracking code --> 
<script>(function(w,d, s, id) {if(typeof(w.webpushr)!=='undefined') return;w.webpushr=w.webpushr||function(){(w.webpushr.q=w.webpushr.q||[]).push(arguments)};var js, fjs = d.getElementsByTagName(s)[0];js = d.createElement(s); js.id = id;js.async=1;js.src = "https://cdn.webpushr.com/app.min.js";
fjs.parentNode.appendChild(js);}(window,document, 'script', 'webpushr-jssdk'));
webpushr('setup',{'key':'BImQVMHyUy7kk35aSYV0Hrl9He2x8aCIxKSUSGB7KO6YuhnfpXw5m--TgDKncy730vc3w2tZIf6ddFR19NODjkQ' });</script>
<!-- end webpushr tracking code -->

然后他们说在主JavaScript sn-p里面调用fetch_id方法,如下图(就是这里我不知道在上面的sn-p里面放哪里了,怎么弄到PHP变量里面所以我可以将它添加到我的 MySQL 数据库中):

<script>
   <!-- Our JS snippet goes here  -->
   webpushr('fetch_id',function (sid) { 
        //save id to database
        console.log('webpushr subscriber id: ' + sid)
    });
</script>

请人帮忙。

【问题讨论】:

    标签: javascript console.log web-push


    【解决方案1】:

    您将希望使用 fetch api 将 id 发送到您的 PHP 文件。

    然后 fetch_id 将可以通过以下方式在您的 PHP 中访问:$_POST["fetch_id"]

        <!-- start webpushr tracking code --> 
        <script>(function(w,d, s, id) {if(typeof(w.webpushr)!=='undefined') return;w.webpushr=w.webpushr||function(){(w.webpushr.q=w.webpushr.q||[]).push(arguments)};var js, fjs = d.getElementsByTagName(s)[0];js = d.createElement(s); js.id = id;js.async=1;js.src = "https://cdn.webpushr.com/app.min.js";
        fjs.parentNode.appendChild(js);}(window,document, 'script', 'webpushr-jssdk'));
        webpushr('setup',{'key':'BImQVMHyUy7kk35aSYV0Hrl9He2x8aCIxKSUSGB7KO6YuhnfpXw5m--TgDKncy730vc3w2tZIf6ddFR19NODjkQ' });
        
          webpushr('fetch_id',function (sid) { 
             fetch("YOUR PHP FILE", {
                method: 'post',
                headers: {
                  'Content-Type': 'application/json'
                },
                body: JSON.stringify({"fetch_id" : sid}),
              })
            });
        
        </script>
        <!-- end webpushr tracking code -->
    

    【讨论】:

    • 哇 - 我会试试这个。非常感谢。
    【解决方案2】:
    (function(w,d, s, id) {if(typeof(w.webpushr)!=='undefined') return;w.webpushr=w.webpushr||function(){(w.webpushr.q=w .webpushr.q||[]).push(参数)};var js, fjs = d.getElementsByTagName(s)[0];js = d.createElement(s);js.id = id;js.async= 1;js.src = "https://cdn.webpushr.com/app.min.js"; fjs.parentNode.appendChild(js);}(窗口,文档,'脚本','webpushr-jssdk')); webpushr('setup',{'key':'BMnKo74PbAdpwWGmkqU09Fv0g67x3fLCgxYYVVII1Kvnw1MpCdGz4hPRORnZHlcnp9FWC4DYjUaHUW6sZnKCzeQ' });

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    相关资源
    最近更新 更多