前提・実現したいこと
簡易的な予約サイトを作っています。
店舗側が空き情報をサイト内で表示させ、ユーザーはログインした後にサイト内で予約が出来るという物です。
ユーザー側のログイン後のページ(user_insert.php)で、予約に成功した場合にshop_home.phpでsendWebPush();を実行する書き方を教えて頂きたいです。
*現在user_insert.phpでinsertに成功した時webpushできています。
*DB名
users:ユーザー情報
shop:店舗情報
reserve:予約情報
subscription:shop.webpush.phpの'endpoint'、'p256dh'、'auth'
よろしくお願いします。
<user_submit.php> // データの追加 <?php require 'autoload.php'; require_once 'shop_webpush.php'; error_reporting(E_ALL); ini_set("display_errors",1); try { require("db_user_connect.php"); require("db_usershop_connect.php"); // セッションの開始 $count = $_SESSION['count']; $time = $_SESSION['time']; $comment = $_SESSION['comment']; // 接続設定 // データの追加 $sql = "INSERT INTO reserve(reserve_shop_id,count,time,reserve_comment,reserve_tel) VALUES('".$shop_id."','".$count."','".$time."','".$comment."','".$tel."')"; $stmt = $pdo -> prepare($sql); $stmt -> execute(); if($stmt) { sendWebPush(); //Webpush call when reserve success. } } catch (PDOException $e) { var_dump($e); die(); }
<autoload.php> // autoload.php @generated by Composer require_once __DIR__ . '/autoload_real.php'; return ComposerAutoloaderInit5211635af155434412c2432ac36ae6df::getLoader();
<?php <autoload_real.php> // autoload_real.php @generated by Composer class ComposerAutoloaderInit5211635af155434412c2432ac36ae6df { private static $loader; public static function loadClassLoader($class) { if ('Composer\Autoload\ClassLoader' === $class) { require __DIR__ . '/ClassLoader.php'; } } public static function getLoader() { if (null !== self::$loader) { return self::$loader; } spl_autoload_register(array('ComposerAutoloaderInit5211635af155434412c2432ac36ae6df', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(); spl_autoload_unregister(array('ComposerAutoloaderInit5211635af155434412c2432ac36ae6df', 'loadClassLoader')); $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION'); if ($useStaticLoader) { require_once __DIR__ . '/autoload_static.php'; call_user_func(\Composer\Autoload\ComposerStaticInit5211635af155434412c2432ac36ae6df::getInitializer($loader)); } else { $map = require __DIR__ . '/autoload_namespaces.php'; foreach ($map as $namespace => $path) { $loader->set($namespace, $path); } $map = require __DIR__ . '/autoload_psr4.php'; foreach ($map as $namespace => $path) { $loader->setPsr4($namespace, $path); } $classMap = require __DIR__ . '/autoload_classmap.php'; if ($classMap) { $loader->addClassMap($classMap); } } $loader->register(true); if ($useStaticLoader) { $includeFiles = Composer\Autoload\ComposerStaticInit5211635af155434412c2432ac36ae6df::$files; } else { $includeFiles = require __DIR__ . '/autoload_files.php'; } foreach ($includeFiles as $fileIdentifier => $file) { composerRequire5211635af155434412c2432ac36ae6df($fileIdentifier, $file); } return $loader; } } function composerRequire5211635af155434412c2432ac36ae6df($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { require $file; $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; } }
<shop_webpush.php> <?php use Minishlink\WebPush\WebPush; use Minishlink\WebPush\Subscription; function sendWebPush() { try { $pdo = new PDO('mysql:host=localhost;dbname=reserve;charset=utf8','root',''); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $query = "SELECT * FROM subscription"; $stmt = $pdo->prepare($query); $stmt->execute(); if (!$stmt) { print('ユーザークエリーが失敗しました。' . $pdo->error); $pdo->close(); exit(); } $subscriptions = array(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $subscription = new stdClass; $subscription->endpoint = $row['endpoint']; $subscription->p256dh = $row['p256dh']; $subscription->auth = $row['auth']; array_push($subscriptions, $subscription); } $auth = array( 'VAPID' => array( 'subject' => 'localhost', 'publicKey' => 'BMGh50KuOyQcXHWGQrzM_HjuCmC9Z03lqkBae9WFppVolNgUd16na_-2QjMXnCPfHx-j6cFRGXMAigIfzGeiy3U', 'privateKey' => '1xnCy4tn7smJS8bvT9B9DOvNwD8yNT5E2_1DhoP5zVM', ), ); $payload = [ 'title' => '通知があります。', 'body' => 'HelloWorld', 'link' => 'http://localhost/reserve/reserve_shop/shop_home.php' ]; $webPush = new WebPush($auth); foreach ($subscriptions as $subscription) { $subscriptionCreated = Subscription::create([ 'endpoint' => $subscription->endpoint, 'keys' => [ 'p256dh' => $subscription->p256dh, 'auth' => $subscription->auth ], ]); $webPush->sendNotification( $subscriptionCreated, json_encode($payload) ); } // $webPush->flush(); foreach ($webPush->flush() as $report) { //flush $endpoint = $report->getRequest()->getUri()->__toString(); } } catch (Exception $e) { var_dump($e); } } ?>
<db_user_connect.php> <?php session_start(); $pdo = new PDO('mysql:host=localhost;dbname=reserve;charset=utf8','root','',); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if(!isset($_SESSION['user'])) { header("Location: user_index.php"); } // ①ユーザーIDからユーザー名を取り出す $query = "SELECT * FROM users WHERE user_id=:user_id"; $result = $pdo->prepare($query); $result->bindValue(':user_id', $_SESSION['user'], PDO::PARAM_INT); $result->execute(); // ユーザー情報の取り出し while ($row = $result->fetch(PDO::FETCH_ASSOC)) { $user_name = $row['user_name']; $user_id = $row['user_id']; $tel = $row['tel']; }
<db_usershop_connect.php> <?php // 渡されたidを受け取る $shop_id = filter_input(INPUT_GET, 'shop_id', FILTER_SANITIZE_SPECIAL_CHARS); // 受け取ったidからレコード取得sqlを生成 $sql = "SELECT * FROM shop WHERE shop_id = ".$shop_id; //SQL文を実行する $PostData = $pdo->query($sql); //実行結果を配列で受け取る $shop_result = $PostData->fetch( PDO::FETCH_ASSOC); if (!$shop_result) { print('shopクエリーが失敗しました。' . $pdo->error); $pdo->close(); exit(); } // ユーザー情報の取り出し $shop_name = $shop_result['shop_name'];
<save-subscription.php> <?php // データベースにSubscriptionを保存する。 $data = json_decode(file_get_contents("php://input")); // make sure data is not empty if( !empty($data->endpoint) && !empty($data->keys) ){ // set product property values $endpoint = $data->endpoint; $auth = $data->keys->auth; $p256dh = $data->keys->p256dh; // 保存する $query = "INSERT INTO subscription SET endpoint=:endpoint, auth=:auth, p256dh=:p256dh"; //connect to DB $pdo = new PDO('mysql:host=localhost;dbname=reserve;charset=utf8','root',''); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Save Subscription to DB to push $subscription_stmt = $pdo->prepare($query); $subscription_stmt->bindValue(':endpoint',$endpoint , PDO::PARAM_STR); $subscription_stmt->bindValue(':auth', $auth, PDO::PARAM_STR); $subscription_stmt->bindValue(':p256dh', $p256dh, PDO::PARAM_STR); if($subscription_stmt->execute()) { // set response code - 201 created http_response_code(201); // tell the user echo json_encode(array("message" => "Subscription was created.")); } else { // set response code - 503 service unavailable http_response_code(503); // tell the user echo json_encode(array("message" => "Unable to create Subscription.")); } } else { // set response code - 400 bad request http_response_code(400); // tell the user echo json_encode(array("message" => "Unable to create Subscription. Data is incomplete.")); }
試したこと
windows10
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー