Skip to content

Instantly share code, notes, and snippets.

@metropolian
Created April 29, 2020 11:41
Show Gist options
  • Save metropolian/75aa3aa6b9a085c5ccdbadded521269f to your computer and use it in GitHub Desktop.
Save metropolian/75aa3aa6b9a085c5ccdbadded521269f to your computer and use it in GitHub Desktop.
Firebase Cloud Messaging PHP CURL
<?php
require_once('configs.php');
require_once('web_header.php');
$PageTitle = "Test SMS";
// Firebase Console -> Project Overview -> Settings -> Cloud Messaging
$Configs['FCM_SERVER_KEY'] = "....";
function CloudMessaging_SendMessage($message, $topic )
{ global $Configs;
$Server_Url = $Configs['FCM_SERVER_URL'];;
if ($Server_Url == '')
$Server_Url = "https://fcm.googleapis.com/fcm/send";
$Key = $Configs['FCM_SERVER_KEY'];
if (is_array($message))
$data = $message;
else
$data = array(
"message" => $message
);
$fields = array(
"to" => "/topics/" . $topic,
'data' => $data,
"priority" => $priority,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Server_Url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: key={$Key}",
'Content-Type: application/json'
));
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);
return $output;
}
$Tx = $_REQUEST;
$func = $_REQUEST['func'];
$mode = $_REQUEST['mode'];
$Id = intval($_REQUEST['id']);
$errors = array();
if ($func == 'submit')
{
$topic = trim( $_REQUEST['topic'] );
$message = trim( $_REQUEST['message'] );
if (count($errors) == 0)
{
if ($mode == '')
{
$res = CloudMessaging_SendMessage($message, $topic);
}
}
}
?>
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="panel-title"><?php echo $PageTitle ?></h2>
</div>
<div class="panel-body">
<?php if (($mode == '') && ($res)) : ?>
<div class="alert alert-success">
<p>Sent result:</p>
<code><?php echo print_r($res, 1) ?></code>
</div>
<?php endif;?>
<?php if (count($errors)) : ?>
<div class="alert alert-warning">
<?php foreach($errors as $error) : ?>
<li><?php echo $error ?></li>
<?php endforeach ?>
</div>
<?php endif;?>
<form class="form-horizontal" method="post" enctype="multipart/form-data">
<input type="hidden" value="submit" name="func" />
<input type="hidden" value="<?php echo $mode ?>" name="mode" />
<input type="hidden" value="<?php echo $Id ?>" name="id" />
<div class="form-group">
<label class="col-sm-3 control-label" for="topic" >Topic</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="topic" name="topic" placeholder="" value="<?php echo $Tx['topic'] ?>" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="message" >Message</label>
<div class="col-sm-6">
<textarea class="form-control" id="message" name="message" rows="5"><?php echo FormatHtml( $Tx['message'] ) ?></textarea>
</div>
</div>
<hr/>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-primary confirm">บันทึก</button>
<a href="<?php echo $TablePage ?>" class="btn btn-warning">ย้อนกลับ</a>
</div>
</div>
</form>
</div>
</div>
<?php
require_once('web_footer.php');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment