Created
June 24, 2018 07:02
-
-
Save TheBojda/98887f71dfe966521a8d2007674f8e5d to your computer and use it in GitHub Desktop.
Minimal push gateway for RocketChat in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// logging the requests for DEBUG | |
/* | |
ob_start(); | |
echo $_SERVER['REQUEST_URI']; | |
$post_data = file_get_contents("php://input"); | |
print_r($post_data); | |
$data = json_decode($post_data); | |
var_dump($data); | |
echo "\n"; | |
echo "token: " . $data->token . '\n'; | |
echo "text: " . $data->options->text . '\n'; | |
file_put_contents(time() . ".log", ob_get_contents()); | |
ob_end_flush(); | |
*/ | |
$data = json_decode(file_get_contents("php://input")); | |
$msg = array( | |
'body' => $data->options->text, | |
'title' => 'ENVIENTA Chat', | |
'type' => 'chat' | |
); | |
$fields = array( | |
'to' => $data->token, | |
'data' => $msg | |
); | |
$headers = array( | |
'Authorization: key=YOUR FCM PUSH API 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 ) ); | |
$result = curl_exec($ch ); | |
curl_close( $ch ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RocketChat sends requests to /push/gcm/send, so a .htaccess file is needed with content something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule (.*)$ /index.php/$1 [L,QSA]
DirectoryIndex index.php