'How to get Webpush subscriberID from console.log into php
I am battling to get the subscriberid into a PHP variable from the console.log(). Webpush have given the js code but unfortunately when asked to assist they said they couldn't. Below is what they share. Please can someone assist me.
The main javascript snippet (This works perfectly).
<!-- 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 -->
Then they say Call fetch_id method inside the main JavaScript snippet, as shown below (It is here that I have no idea where to put it in the above snippet, and how to get it into a PHP variable so I can add it to my MySQL database):
<script>
<!-- Our JS snippet goes here -->
webpushr('fetch_id',function (sid) {
//save id to database
console.log('webpushr subscriber id: ' + sid)
});
</script>
Please someone help.
Solution 1:[1]
You will want to use the fetch api to send the id to your PHP file.
The fetch_id will then be accessible in your PHP via: $_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 -->
Solution 2:[2]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Sattaban Khomeeklang |
