加载页面后,Curl_exec立即启动。如何解决?

时间:2018-04-10 12:43:32

标签: php html curl

我正在构建一个基本网页,其中包含一些将Firebase推送通知发送到Android应用的表单。除了一件事,一切都很好。每次加载页面时,都会在页面启动时发送通知。也就是说,甚至在填写表格之前。

然后我添加了if条件,以便在表单为空(例如$_POST['message'] == NULL)时,不会触发curl_exec。它工作得很好,但是在加载页面时我有很多通知:消息。我可以很容易地忽略它,但我现在可以看到,当我刷新页面时,表单中的数据保持不变。任何人都可以帮我解决这个问题吗?

代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Firebase Push Notification</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>

  <div class="container">
    <div class="row">
      <div class="col-lg-6">
    
        <hr />
        <form action="" method="post">
         
          <div class="form-group">
            <label for="title">Title:</label>
            <input type="text" required="" class="form-control" id="title" placeholder="Title" name="title">
          </div>
          <div class="form-group">
            <label for="message">Content:</label>
            <textarea required="" class="form-control" rows="2" id="message" placeholder="Content" name="message"></textarea>
          </div>

          <div class="form-group">

            <label for="destinatia">Where to open?:</label>
            <select class="form-control" name="destinatia" required> 
                            <option value="first_page">First page</option> 
                            <option value="events">Events</option> 
                            </select>
          </div>

          <button type="submit" class="btn btn-info">Send</button>
        </form>

      </div>

      <?php
define('API_ACCESS_KEY', 'AAAA0xxxxxxxxxxxxxxxxxxxx');

$msg = array
(
'body' => $_POST['message'],
'title' => $_POST['title'],
'priority' => 'high',
'vibrate' => 100,
'sound' => 'default',
'click_action' => $_POST['destinatia'],
//'time_to_live' => 3600
);

$fields = array('to' => '/topics/CHECK', 'notification' => $msg);

$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
if($_POST['message'] == NULL){
	echo 'Empty';}
else {
	$result = curl_exec($ch);
	echo 'Not empty';
	curl_close($ch);
	echo '<h2>Checking:</h2><hr/><h3>Sent:</h3><p><pre>';
	echo json_encode($fields,JSON_PRETTY_PRINT);
	echo '</pre></p><h3>ID</h3><p><pre>';
	echo $result;
	echo '</pre></p>';
	}
?>

0 个答案:

没有答案