Skip to content

Instantly share code, notes, and snippets.

@TheBojda
Created June 24, 2018 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheBojda/98887f71dfe966521a8d2007674f8e5d to your computer and use it in GitHub Desktop.
Save TheBojda/98887f71dfe966521a8d2007674f8e5d to your computer and use it in GitHub Desktop.
Minimal push gateway for RocketChat in PHP
<?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 );
?>
@TheBojda
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment