webclient_example.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @file: XMPPHP Cli example
  4. *
  5. * @info: If this script doesn't work, are you running 64-bit PHP with < 5.2.6?
  6. */
  7. /**
  8. * Activate full error reporting
  9. * error_reporting(E_ALL & E_STRICT);
  10. *
  11. * XMPPHP Log levels:
  12. *
  13. * LEVEL_ERROR = 0;
  14. * LEVEL_WARNING = 1;
  15. * LEVEL_INFO = 2;
  16. * LEVEL_DEBUG = 3;
  17. * LEVEL_VERBOSE = 4;
  18. */
  19. session_start();
  20. header('content-type', 'plain/text');
  21. $conf = [
  22. 'server' => 'talk.google.com',
  23. 'port' => 5222,
  24. 'username' => 'username',
  25. 'password' => 'password',
  26. 'proto' => 'xmpphp',
  27. 'domain' => 'gmail.com',
  28. 'printlog' => true,
  29. 'loglevel' => XMPPHP\Log::LEVEL_VERBOSE,
  30. ];
  31. // Easy and simple for access to variables with their names
  32. extract($conf);
  33. $conn = new XMPPHP\XMPP($server, $port, $username, $password, $proto, $domain, $printlog, $loglevel);
  34. $conn->autoSubscribe();
  35. try {
  36. if (isset($_SESSION['messages'])) {
  37. foreach ($_SESSION['messages'] as $message) {
  38. echo $message;
  39. flush();
  40. }
  41. } else {
  42. $_SESSION['messages'] = [];
  43. }
  44. $conn->connect();
  45. $events = ['message', 'presence', 'end_stream', 'session_start', 'vcard'];
  46. $payloads = $conn->processUntil($events);
  47. foreach ($payloads as $result) {
  48. list($event, $data) = $result;
  49. if (isset($data)) {
  50. extract($data);
  51. }
  52. switch ($event) {
  53. case 'message':
  54. if (!$body) {
  55. break;
  56. }
  57. $cmd = explode(' ', $body);
  58. $msg = str_repeat('-', 80);
  59. $msg .= "\nMessage from: $from\n";
  60. if (isset($subject)) {
  61. $msg .= "Subject: $subject\n";
  62. }
  63. $msg .= $body . "\n";
  64. $msg .= str_repeat('-', 80);
  65. echo "<pre>$msg</pre>";
  66. if (isset($cmd[0])) {
  67. if ($cmd[0] == 'quit') {
  68. $conn->disconnect();
  69. }
  70. if ($cmd[0] == 'break') {
  71. $conn->send('</end>');
  72. }
  73. }
  74. $_SESSION['messages'][] = $msg;
  75. flush();
  76. break;
  77. case 'presence':
  78. echo "Presence: $from [$show] $status\n";
  79. break;
  80. case 'session_start':
  81. echo "Session start\n";
  82. $conn->getRoster();
  83. $conn->presence('Quasar!');
  84. break;
  85. }
  86. }
  87. } catch (XMPPHP\Exception $e) {
  88. die($e->getMessage());
  89. }
  90. $conn->saveSession();
  91. echo '<img src="http://xmpp.org/images/xmpp.png" onload="window.location.reload()" />';