質問編集履歴

1

ファイルの追加

2019/05/17 10:08

投稿

ariiiiiga
ariiiiiga

スコア66

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