Skip to content

Instantly share code, notes, and snippets.

@MarQuisKnox
Created June 22, 2015 13:07
Show Gist options
  • Save MarQuisKnox/34b8e7c0d69a5d8023ea to your computer and use it in GitHub Desktop.
Save MarQuisKnox/34b8e7c0d69a5d8023ea to your computer and use it in GitHub Desktop.
Send XMPP Messages using PHP
<?php
error_reporting( E_ALL & ~E_STRICT );
ini_set( 'display_errors', true );
$path = dirname( __FILE__ ).'/library';
set_include_path(
get_include_path() . PATH_SEPARATOR . $path
);
require_once('Zend/Loader/Autoloader.php');
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
// constants
define('XMPP_PRESENCE', false);
define('XMPP_HOST', 'example.com');
define('XMPP_PORT', 5222);
define('XMPP_USERNAME', 'username');
define('XMPP_PASSWORD', 'password');
define('XMPP_RESOURCE', 'xmpphp');
define('XMPP_SERVER', null);
define('XMPP_PRINT_LOG', true);
define('XMPP_ERROR_LEVEL', XMPPHP_Log::LEVEL_VERBOSE);
// users
$users = array(
'employee@example.com',
'ceo@example.com',
);
// message
$message = 'This is a test message...'. PHP_EOL . 'File: '.__FILE__ . PHP_EOL . 'Line: '.__LINE__;
echo '<pre>';
$conn = new XMPPHP_XMPP(
XMPP_HOST,
XMPP_PORT,
XMPP_USERNAME,
XMPP_PASSWORD,
XMPP_RESOURCE,
XMPP_SERVER,
XMPP_PRINT_LOG,
XMPP_ERROR_LEVEL
);
try {
$conn->connect();
$conn->processUntil('session_start');
if( XMPP_PRESENCE ) {
$conn->presence();
}
if( !empty( $users ) ) {
foreach( $users AS $key => $value ) {
$conn->message( $value, $message );
}
}
$conn->disconnect();
} catch(XMPPHP_Exception $e) {
die($e->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment