cli_longrun_example.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. require '../vendor/autoload.php';
  20. $conf = array(
  21. 'server' => 'talk.google.com',
  22. 'port' => 5222,
  23. 'username' => 'username',
  24. 'password' => 'password',
  25. 'proto' => 'xmpphp',
  26. 'domain' => 'gmail.com',
  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\XMPP($server, $port, $username, $password, $proto, $domain, $printlog, $loglevel);
  33. $conn->autoSubscribe();
  34. $vcard_request = array();
  35. try {
  36. $conn->connect();
  37. while (!$conn->isDisconnected()) {
  38. $events = array('message', 'presence', 'end_stream', 'session_start', 'vcard');
  39. $payloads = $conn->processUntil($events);
  40. foreach ($payloads as $result) {
  41. list($event, $data) = $result;
  42. if (isset($data)) {
  43. extract($data);
  44. }
  45. switch ($event) {
  46. case 'message':
  47. if (!$body) {
  48. break;
  49. }
  50. echo str_repeat('-', 80);
  51. echo "Message from: $from";
  52. if (isset($subject)) {
  53. echo "Subject: $subject";
  54. }
  55. echo $body;
  56. echo str_repeat('-', 80);
  57. $cmd = explode(' ', $body);
  58. $body = "Mi no entender! '$body'";
  59. $conn->message($from, $body, $type);
  60. if (isset($cmd[0])) {
  61. if ($cmd[0] == 'quit') {
  62. $conn->disconnect();
  63. }
  64. if ($cmd[0] == 'break') {
  65. $conn->send('</end>');
  66. }
  67. if ($cmd[0] == 'vcard') {
  68. if (!isset($cmd[1])) {
  69. $cmd[1] = $conn->user;
  70. }
  71. // Take a note which user requested which vcard
  72. $vcard_request[$from] = $cmd[1];
  73. // Request the vcard
  74. $conn->getVCard($cmd[1]);
  75. }
  76. }
  77. break;
  78. case 'presence':
  79. echo "Presence: $from [$show] $status\n";
  80. break;
  81. case 'session_start':
  82. echo "Session start\n";
  83. $conn->getRoster();
  84. $conn->presence('Quasar!');
  85. break;
  86. case 'vcard':
  87. $deliver = array_keys($vcard_request, $from);
  88. $msg = '';
  89. foreach ($data as $key => $item) {
  90. $msg .= $key . ': ';
  91. if (is_array($item)) {
  92. $msg .= "\n";
  93. foreach ($item as $subkey => $subitem) {
  94. $msg .= ' ' . $subkey . ':' . $subitem . "\n";
  95. }
  96. } else {
  97. $msg .= $item . "\n";
  98. }
  99. }
  100. foreach ($deliver as $sendjid) {
  101. unset($vcard_request[$sendjid]);
  102. $conn->message($sendjid, $msg, 'chat');
  103. }
  104. break;
  105. }
  106. }
  107. }
  108. } catch (XMPPHP\Exception $e) {
  109. die($e->getMessage());
  110. }