cli_longrun_example_bosh.php 2.2 KB

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