xmpphp/examples/webclient_example.php

115 lines
2.7 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* @file: XMPPHP Cli example
*
* @info: If this script doesn't work, are you running 64-bit PHP with < 5.2.6?
*/
/**
* Activate full error reporting
* error_reporting(E_ALL & E_STRICT);
*
* XMPPHP Log levels:
*
* LEVEL_ERROR = 0;
* LEVEL_WARNING = 1;
* LEVEL_INFO = 2;
* LEVEL_DEBUG = 3;
* LEVEL_VERBOSE = 4;
*/
session_start();
header('content-type', 'plain/text');
2019-11-02 23:36:21 +00:00
$conf = [
'server' => 'talk.google.com',
'port' => 5222,
'username' => 'username',
'password' => 'password',
'proto' => 'xmpphp',
'domain' => 'gmail.com',
'printlog' => true,
'loglevel' => XMPPHP\Log::LEVEL_VERBOSE,
];
// Easy and simple for access to variables with their names
extract($conf);
$conn = new XMPPHP\XMPP($server, $port, $username, $password, $proto, $domain, $printlog, $loglevel);
$conn->autoSubscribe();
try {
if (isset($_SESSION['messages'])) {
foreach ($_SESSION['messages'] as $message) {
echo $message;
flush();
}
} else {
2019-11-02 23:36:21 +00:00
$_SESSION['messages'] = [];
}
2019-11-02 23:36:21 +00:00
$conn->connect();
$events = ['message', 'presence', 'end_stream', 'session_start', 'vcard'];
$payloads = $conn->processUntil($events);
foreach ($payloads as $result) {
list($event, $data) = $result;
if (isset($data)) {
extract($data);
}
switch ($event) {
2019-11-02 23:36:21 +00:00
case 'message':
2019-11-02 23:36:21 +00:00
if (!$body) {
break;
}
2019-11-02 23:36:21 +00:00
$cmd = explode(' ', $body);
$msg = str_repeat('-', 80);
$msg .= "\nMessage from: $from\n";
2019-11-02 23:36:21 +00:00
if (isset($subject)) {
$msg .= "Subject: $subject\n";
}
2019-11-02 23:36:21 +00:00
$msg .= $body . "\n";
$msg .= str_repeat('-', 80);
echo "<pre>$msg</pre>";
2019-11-02 23:36:21 +00:00
if (isset($cmd[0])) {
if ($cmd[0] == 'quit') {
$conn->disconnect();
}
2019-11-02 23:36:21 +00:00
if ($cmd[0] == 'break') {
$conn->send('</end>');
}
}
$_SESSION['messages'][] = $msg;
flush();
break;
2019-11-02 23:36:21 +00:00
case 'presence':
2019-11-02 23:36:21 +00:00
echo "Presence: $from [$show] $status\n";
break;
2019-11-02 23:36:21 +00:00
case 'session_start':
2019-11-02 23:36:21 +00:00
echo "Session start\n";
$conn->getRoster();
$conn->presence('Quasar!');
break;
}
}
} catch (XMPPHP\Exception $e) {
die($e->getMessage());
}
$conn->saveSession();
echo '<img src="http://xmpp.org/images/xmpp.png" onload="window.location.reload()" />';