質問編集履歴

2

ファイルの追加

2019/05/17 10:10

投稿

ariiiiiga
ariiiiiga

スコア66

test CHANGED
File without changes
test CHANGED
@@ -10,17 +10,67 @@
10
10
 
11
11
  *現在user_insert.phpでinsertに成功した時webpushできています。
12
12
 
13
-
13
+ *DB名
14
-
14
+
15
- ### 該当のソスコ
15
+ users:ユ情報
16
+
16
-
17
+ shop:店舗情報
18
+
17
-
19
+ reserve:予約情報
20
+
18
-
21
+ subscription:shop.webpush.phpの'endpoint'、'p256dh'、'auth'
22
+
23
+
24
+
25
+ よろしくお願いします。
26
+
27
+
28
+
19
- ```ここに言語を入力
29
+ ```ここに言語を入力
20
-
30
+
21
- <user_insert.php>
31
+ <user_submit.php>
32
+
22
-
33
+ // データの追加
34
+
35
+ <?php
36
+
37
+ require 'autoload.php';
38
+
39
+ require_once 'shop_webpush.php';
40
+
41
+
42
+
43
+
44
+
45
+ error_reporting(E_ALL);
46
+
47
+ ini_set("display_errors",1);
48
+
49
+
50
+
51
+ try {
52
+
53
+ require("db_user_connect.php");
54
+
55
+ require("db_usershop_connect.php");
56
+
57
+
58
+
59
+ // セッションの開始
60
+
61
+ $count = $_SESSION['count'];
62
+
63
+ $time = $_SESSION['time'];
64
+
65
+ $comment = $_SESSION['comment'];
66
+
67
+ // 接続設定
68
+
69
+
70
+
71
+ // データの追加
72
+
23
- $sql = "INSERT INTO mydb(id,comment) VALUES('".$id."','".$comment."')";
73
+ $sql = "INSERT INTO reserve(reserve_shop_id,count,time,reserve_comment,reserve_tel) VALUES('".$shop_id."','".$count."','".$time."','".$comment."','".$tel."')";
24
74
 
25
75
  $stmt = $pdo -> prepare($sql);
26
76
 
@@ -28,13 +78,559 @@
28
78
 
29
79
  if($stmt) {
30
80
 
31
- sendWebPush(); //Webpush call when insert success.
81
+ sendWebPush(); //Webpush call when reserve success.
32
82
 
33
83
  }
34
84
 
85
+
86
+
87
+ } catch (PDOException $e) {
88
+
89
+ var_dump($e);
90
+
91
+ die();
92
+
93
+ }
94
+
35
95
  ```
36
96
 
37
-
97
+ ```ここに言語を入力
98
+
99
+ <autoload.php>
100
+
101
+
102
+
103
+ // autoload.php @generated by Composer
104
+
105
+
106
+
107
+ require_once __DIR__ . '/autoload_real.php';
108
+
109
+
110
+
111
+ return ComposerAutoloaderInit5211635af155434412c2432ac36ae6df::getLoader();
112
+
113
+
114
+
115
+ ```
116
+
117
+
118
+
119
+ ```ここに言語を入力
120
+
121
+ <?php
122
+
123
+ <autoload_real.php>
124
+
125
+ // autoload_real.php @generated by Composer
126
+
127
+
128
+
129
+ class ComposerAutoloaderInit5211635af155434412c2432ac36ae6df
130
+
131
+ {
132
+
133
+ private static $loader;
134
+
135
+
136
+
137
+ public static function loadClassLoader($class)
138
+
139
+ {
140
+
141
+ if ('Composer\Autoload\ClassLoader' === $class) {
142
+
143
+ require __DIR__ . '/ClassLoader.php';
144
+
145
+ }
146
+
147
+ }
148
+
149
+
150
+
151
+ public static function getLoader()
152
+
153
+ {
154
+
155
+ if (null !== self::$loader) {
156
+
157
+ return self::$loader;
158
+
159
+ }
160
+
161
+
162
+
163
+ spl_autoload_register(array('ComposerAutoloaderInit5211635af155434412c2432ac36ae6df', 'loadClassLoader'), true, true);
164
+
165
+ self::$loader = $loader = new \Composer\Autoload\ClassLoader();
166
+
167
+ spl_autoload_unregister(array('ComposerAutoloaderInit5211635af155434412c2432ac36ae6df', 'loadClassLoader'));
168
+
169
+
170
+
171
+ $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION');
172
+
173
+ if ($useStaticLoader) {
174
+
175
+ require_once __DIR__ . '/autoload_static.php';
176
+
177
+
178
+
179
+ call_user_func(\Composer\Autoload\ComposerStaticInit5211635af155434412c2432ac36ae6df::getInitializer($loader));
180
+
181
+ } else {
182
+
183
+ $map = require __DIR__ . '/autoload_namespaces.php';
184
+
185
+ foreach ($map as $namespace => $path) {
186
+
187
+ $loader->set($namespace, $path);
188
+
189
+ }
190
+
191
+
192
+
193
+ $map = require __DIR__ . '/autoload_psr4.php';
194
+
195
+ foreach ($map as $namespace => $path) {
196
+
197
+ $loader->setPsr4($namespace, $path);
198
+
199
+ }
200
+
201
+
202
+
203
+ $classMap = require __DIR__ . '/autoload_classmap.php';
204
+
205
+ if ($classMap) {
206
+
207
+ $loader->addClassMap($classMap);
208
+
209
+ }
210
+
211
+ }
212
+
213
+
214
+
215
+ $loader->register(true);
216
+
217
+
218
+
219
+ if ($useStaticLoader) {
220
+
221
+ $includeFiles = Composer\Autoload\ComposerStaticInit5211635af155434412c2432ac36ae6df::$files;
222
+
223
+ } else {
224
+
225
+ $includeFiles = require __DIR__ . '/autoload_files.php';
226
+
227
+ }
228
+
229
+ foreach ($includeFiles as $fileIdentifier => $file) {
230
+
231
+ composerRequire5211635af155434412c2432ac36ae6df($fileIdentifier, $file);
232
+
233
+ }
234
+
235
+
236
+
237
+ return $loader;
238
+
239
+ }
240
+
241
+ }
242
+
243
+
244
+
245
+ function composerRequire5211635af155434412c2432ac36ae6df($fileIdentifier, $file)
246
+
247
+ {
248
+
249
+ if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
250
+
251
+ require $file;
252
+
253
+
254
+
255
+ $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
256
+
257
+ }
258
+
259
+ }
260
+
261
+
262
+
263
+ ```
264
+
265
+ ```ここに言語を入力
266
+
267
+ <shop_webpush.php>
268
+
269
+ <?php
270
+
271
+ use Minishlink\WebPush\WebPush;
272
+
273
+ use Minishlink\WebPush\Subscription;
274
+
275
+
276
+
277
+ function sendWebPush() {
278
+
279
+ try {
280
+
281
+
282
+
283
+ $pdo = new PDO('mysql:host=localhost;dbname=reserve;charset=utf8','root','');
284
+
285
+ $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
286
+
287
+ $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
288
+
289
+
290
+
291
+ $query = "SELECT * FROM subscription";
292
+
293
+ $stmt = $pdo->prepare($query);
294
+
295
+ $stmt->execute();
296
+
297
+
298
+
299
+ if (!$stmt) {
300
+
301
+ print('ユーザークエリーが失敗しました。' . $pdo->error);
302
+
303
+ $pdo->close();
304
+
305
+ exit();
306
+
307
+ }
308
+
309
+
310
+
311
+ $subscriptions = array();
312
+
313
+ while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
314
+
315
+ $subscription = new stdClass;
316
+
317
+ $subscription->endpoint = $row['endpoint'];
318
+
319
+ $subscription->p256dh = $row['p256dh'];
320
+
321
+ $subscription->auth = $row['auth'];
322
+
323
+ array_push($subscriptions, $subscription);
324
+
325
+ }
326
+
327
+
328
+
329
+
330
+
331
+ $auth = array(
332
+
333
+ 'VAPID' => array(
334
+
335
+ 'subject' => 'localhost',
336
+
337
+ 'publicKey' => 'BMGh50KuOyQcXHWGQrzM_HjuCmC9Z03lqkBae9WFppVolNgUd16na_-2QjMXnCPfHx-j6cFRGXMAigIfzGeiy3U',
338
+
339
+ 'privateKey' => '1xnCy4tn7smJS8bvT9B9DOvNwD8yNT5E2_1DhoP5zVM',
340
+
341
+ ),
342
+
343
+ );
344
+
345
+
346
+
347
+ $payload = [
348
+
349
+ 'title' => '通知があります。',
350
+
351
+ 'body' => 'HelloWorld',
352
+
353
+ 'link' => 'http://localhost/reserve/reserve_shop/shop_home.php'
354
+
355
+ ];
356
+
357
+
358
+
359
+ $webPush = new WebPush($auth);
360
+
361
+
362
+
363
+ foreach ($subscriptions as $subscription) {
364
+
365
+ $subscriptionCreated = Subscription::create([
366
+
367
+ 'endpoint' => $subscription->endpoint,
368
+
369
+ 'keys' => [
370
+
371
+ 'p256dh' => $subscription->p256dh,
372
+
373
+ 'auth' => $subscription->auth
374
+
375
+ ],
376
+
377
+ ]);
378
+
379
+ $webPush->sendNotification(
380
+
381
+ $subscriptionCreated,
382
+
383
+ json_encode($payload)
384
+
385
+ );
386
+
387
+ }
388
+
389
+
390
+
391
+ // $webPush->flush();
392
+
393
+
394
+
395
+ foreach ($webPush->flush() as $report) {
396
+
397
+ //flush
398
+
399
+ $endpoint = $report->getRequest()->getUri()->__toString();
400
+
401
+
402
+
403
+ }
404
+
405
+ } catch (Exception $e) {
406
+
407
+ var_dump($e);
408
+
409
+ }
410
+
411
+
412
+
413
+ }
414
+
415
+ ?>
416
+
417
+
418
+
419
+ ```
420
+
421
+
422
+
423
+ ```ここに言語を入力
424
+
425
+ <db_user_connect.php>
426
+
427
+ <?php
428
+
429
+ session_start();
430
+
431
+ $pdo = new PDO('mysql:host=localhost;dbname=reserve;charset=utf8','root','',);
432
+
433
+ $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
434
+
435
+ $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
436
+
437
+
438
+
439
+ if(!isset($_SESSION['user'])) {
440
+
441
+ header("Location: user_index.php");
442
+
443
+ }
444
+
445
+
446
+
447
+ // ①ユーザーIDからユーザー名を取り出す
448
+
449
+ $query = "SELECT * FROM users WHERE user_id=:user_id";
450
+
451
+ $result = $pdo->prepare($query);
452
+
453
+ $result->bindValue(':user_id', $_SESSION['user'], PDO::PARAM_INT);
454
+
455
+ $result->execute();
456
+
457
+
458
+
459
+ // ユーザー情報の取り出し
460
+
461
+ while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
462
+
463
+ $user_name = $row['user_name'];
464
+
465
+ $user_id = $row['user_id'];
466
+
467
+ $tel = $row['tel'];
468
+
469
+ }
470
+
471
+
472
+
473
+ ```
474
+
475
+
476
+
477
+ ```ここに言語を入力
478
+
479
+ <db_usershop_connect.php>
480
+
481
+ <?php
482
+
483
+ // 渡されたidを受け取る
484
+
485
+ $shop_id = filter_input(INPUT_GET, 'shop_id', FILTER_SANITIZE_SPECIAL_CHARS);
486
+
487
+ // 受け取ったidからレコード取得sqlを生成
488
+
489
+ $sql = "SELECT * FROM shop WHERE shop_id = ".$shop_id;
490
+
491
+ //SQL文を実行する
492
+
493
+ $PostData = $pdo->query($sql);
494
+
495
+ //実行結果を配列で受け取る
496
+
497
+ $shop_result = $PostData->fetch( PDO::FETCH_ASSOC);
498
+
499
+
500
+
501
+ if (!$shop_result) {
502
+
503
+ print('shopクエリーが失敗しました。' . $pdo->error);
504
+
505
+ $pdo->close();
506
+
507
+ exit();
508
+
509
+ }
510
+
511
+ // ユーザー情報の取り出し
512
+
513
+ $shop_name = $shop_result['shop_name'];
514
+
515
+
516
+
517
+ ```
518
+
519
+
520
+
521
+ ```ここに言語を入力
522
+
523
+ <save-subscription.php>
524
+
525
+ <?php
526
+
527
+
528
+
529
+ // データベースにSubscriptionを保存する。
530
+
531
+ $data = json_decode(file_get_contents("php://input"));
532
+
533
+ // make sure data is not empty
534
+
535
+ if(
536
+
537
+ !empty($data->endpoint) &&
538
+
539
+ !empty($data->keys)
540
+
541
+ ){
542
+
543
+
544
+
545
+ // set product property values
546
+
547
+ $endpoint = $data->endpoint;
548
+
549
+ $auth = $data->keys->auth;
550
+
551
+ $p256dh = $data->keys->p256dh;
552
+
553
+
554
+
555
+ // 保存する
556
+
557
+ $query = "INSERT INTO
558
+
559
+ subscription
560
+
561
+ SET
562
+
563
+ endpoint=:endpoint, auth=:auth, p256dh=:p256dh";
564
+
565
+ //connect to DB
566
+
567
+ $pdo = new PDO('mysql:host=localhost;dbname=reserve;charset=utf8','root','');
568
+
569
+ $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
570
+
571
+ $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
572
+
573
+
574
+
575
+
576
+
577
+ // Save Subscription to DB to push
578
+
579
+ $subscription_stmt = $pdo->prepare($query);
580
+
581
+ $subscription_stmt->bindValue(':endpoint',$endpoint , PDO::PARAM_STR);
582
+
583
+ $subscription_stmt->bindValue(':auth', $auth, PDO::PARAM_STR);
584
+
585
+ $subscription_stmt->bindValue(':p256dh', $p256dh, PDO::PARAM_STR);
586
+
587
+
588
+
589
+ if($subscription_stmt->execute()) {
590
+
591
+ // set response code - 201 created
592
+
593
+ http_response_code(201);
594
+
595
+
596
+
597
+ // tell the user
598
+
599
+ echo json_encode(array("message" => "Subscription was created."));
600
+
601
+ } else {
602
+
603
+ // set response code - 503 service unavailable
604
+
605
+ http_response_code(503);
606
+
607
+
608
+
609
+ // tell the user
610
+
611
+ echo json_encode(array("message" => "Unable to create Subscription."));
612
+
613
+ }
614
+
615
+ } else {
616
+
617
+
618
+
619
+ // set response code - 400 bad request
620
+
621
+ http_response_code(400);
622
+
623
+
624
+
625
+ // tell the user
626
+
627
+ echo json_encode(array("message" => "Unable to create Subscription. Data is incomplete."));
628
+
629
+ }
630
+
631
+
632
+
633
+ ```
38
634
 
39
635
  ### 試したこと
40
636
 

1

実現したいことの変更

2019/05/17 10:10

投稿

ariiiiiga
ariiiiiga

スコア66

test CHANGED
File without changes
test CHANGED
@@ -4,17 +4,11 @@
4
4
 
5
5
  店舗側が空き情報をサイト内で表示させ、ユーザーはログインした後にサイト内で予約が出来るという物です。
6
6
 
7
- ユーザー側のログイン後のページ(user_insert.php)で、予約に成功した場合webpush通知を店舗側のshop_home.phpで通知させたいですがどのようにコードを作ればいいかわからなくなってしまいました
7
+ ユーザー側のログイン後のページ(user_insert.php)で、予約に成功した場合shop_home.phpでsendWebPush();を実行する書き方を教えて頂きたいです。
8
8
 
9
9
 
10
10
 
11
11
  *現在user_insert.phpでinsertに成功した時webpushできています。
12
-
13
-
14
-
15
- わかる方よろしくお願いし致します。
16
-
17
-
18
12
 
19
13
 
20
14