cli_longrun_example.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. $conf = [
  20. 'server' => 'im.server.tld',
  21. 'port' => 5222,
  22. 'username' => 'username',
  23. 'password' => 'password',
  24. 'proto' => 'xmpphp',
  25. 'domain' => 'domain.net',
  26. 'printlog' => true,
  27. 'loglevel' => XMPPHP\Log::LEVEL_VERBOSE,
  28. ];
  29. // Easy and simple for access to variables with their names
  30. extract($conf);
  31. $conn = new XMPPHP\XMPP($server, $port, $username, $password, $proto, $domain, $printlog, $loglevel);
  32. $conn->autoSubscribe();
  33. $vcard_request = [];
  34. try {
  35. $conn->connect();
  36. while (!$conn->isDisconnected()) {
  37. $events = ['message', 'presence', 'end_stream', 'session_start', 'vcard'];
  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. if ($cmd[0] == 'vcard') {
  67. if (!isset($cmd[1])) {
  68. $cmd[1] = $conn->user;
  69. }
  70. // Take a note which user requested which vcard
  71. $vcard_request[$from] = $cmd[1];
  72. // Request the vcard
  73. $conn->getVCard($cmd[1]);
  74. }
  75. }
  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. case 'vcard':
  86. $deliver = array_keys($vcard_request, $from);
  87. $msg = '';
  88. foreach ($data as $key => $item) {
  89. $msg .= $key . ': ';
  90. if (is_array($item)) {
  91. $msg .= "\n";
  92. foreach ($item as $subkey => $subitem) {
  93. $msg .= ' ' . $subkey . ':' . $subitem . "\n";
  94. }
  95. } else {
  96. $msg .= $item . "\n";
  97. }
  98. }
  99. foreach ($deliver as $sendjid) {
  100. unset($vcard_request[$sendjid]);
  101. $conn->message($sendjid, $msg, 'chat');
  102. }
  103. break;
  104. }
  105. }
  106. }
  107. } catch (XMPPHP\Exception $e) {
  108. die($e->getMessage());
  109. }