webclient_example.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. require '../vendor/autoload.php';
  22. $conf = array(
  23. 'server' => 'talk.google.com',
  24. 'port' => 5222,
  25. 'username' => 'username',
  26. 'password' => 'password',
  27. 'proto' => 'xmpphp',
  28. 'domain' => 'gmail.com',
  29. 'printlog' => true,
  30. 'loglevel' => XMPPHP\Log::LEVEL_VERBOSE,
  31. );
  32. // Easy and simple for access to variables with their names
  33. extract($conf);
  34. $conn = new XMPPHP\XMPP($server, $port, $username, $password, $proto, $domain, $printlog, $loglevel);
  35. $conn->autoSubscribe();
  36. try {
  37. if (isset($_SESSION['messages'])) {
  38. foreach ($_SESSION['messages'] as $message) {
  39. echo $message;
  40. flush();
  41. }
  42. } else {
  43. $_SESSION['messages'] = array();
  44. }
  45. $conn->connect('http://server.tld:5280/xmpp-httpbind', 1, true);
  46. $events = array('message', 'presence', 'end_stream', 'session_start', 'vcard');
  47. $payloads = $conn->processUntil($events);
  48. foreach ($payloads as $result) {
  49. list($event, $data) = $result;
  50. if (isset($data)) {
  51. extract($data);
  52. }
  53. switch ($event) {
  54. case 'message':
  55. if (!$body) {
  56. break;
  57. }
  58. $cmd = explode(' ', $body);
  59. $msg = str_repeat('-', 80);
  60. $msg .= "\nMessage from: $from\n";
  61. if (isset($subject)) {
  62. $msg .= "Subject: $subject\n";
  63. }
  64. $msg .= $body . "\n";
  65. $msg .= str_repeat('-', 80);
  66. echo "<pre>$msg</pre>";
  67. if (isset($cmd[0])) {
  68. if ($cmd[0] == 'quit') {
  69. $conn->disconnect();
  70. }
  71. if ($cmd[0] == 'break') {
  72. $conn->send('</end>');
  73. }
  74. }
  75. $_SESSION['messages'][] = $msg;
  76. flush();
  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. }
  87. }
  88. } catch (XMPPHP\Exception $e) {
  89. die($e->getMessage());
  90. }
  91. $conn->saveSession();
  92. echo '<img src="http://xmpp.org/images/xmpp.png" onload="window.location.reload()" />';