cli_longrun_example_bosh.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @file: XMPPHP Cli example BOSH
  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. $conf = [
  20. 'host' => 'server.tld',
  21. 'port' => 5280,
  22. 'username' => 'username',
  23. 'password' => 'password',
  24. 'resource' => 'xmpphp',
  25. 'server' => 'http://server.tld:5280/xmpp-httpbind',
  26. 'print_log' => true,
  27. 'log_level' => XMPPHP\Log::LEVEL_VERBOSE,
  28. ];
  29. // Easy and simple for access to variables with their names
  30. extract($conf);
  31. $conn = new XMPPHP\BOSH($host, $port, $username, $password, $proto, $server, $print_log, $log_level);
  32. $conn->autoSubscribe();
  33. try {
  34. $conn->connect();
  35. while (!$conn->isDisconnected()) {
  36. $events = ['message', 'presence', 'end_stream', 'session_start'];
  37. $payloads = $conn->processUntil($events);
  38. foreach ($payloads as $result) {
  39. list($event, $data) = $result;
  40. if (isset($data)) {
  41. extract($data);
  42. }
  43. switch ($event) {
  44. case 'message':
  45. if (!$body) {
  46. break;
  47. }
  48. echo str_repeat('-', 80);
  49. echo "Message from: $from";
  50. if (isset($subject)) {
  51. echo "Subject: $subject";
  52. }
  53. echo $body;
  54. echo str_repeat('-', 80);
  55. $cmd = explode(' ', $body);
  56. $body = "Mi no entender! '$body'";
  57. $conn->message($from, $body, $type);
  58. if (isset($cmd[0])) {
  59. if ($cmd[0] == 'quit') {
  60. $conn->disconnect();
  61. }
  62. if ($cmd[0] == 'break') {
  63. $conn->send('</end>');
  64. }
  65. }
  66. break;
  67. case 'presence':
  68. echo "Presence: $from [$show] $status\n";
  69. break;
  70. case 'session_start':
  71. echo "Session start\n";
  72. $conn->getRoster();
  73. $conn->presence('Quasar!');
  74. break;
  75. }
  76. }
  77. }
  78. } catch (XMPPHP\Exception $e) {
  79. die($e->getMessage());
  80. }