+ -
当前位置:首页 → 问答吧 → Gtalk PHP API 简单例子(基于XMPP)

Gtalk PHP API 简单例子(基于XMPP)

时间:2011-07-26

来源:互联网

转:JessonChan

Gtalk PHP API 简单例子(基于XMPP)


1.下载库文件
传送门:http://code.google.com/p/xmpphp/
或者使用svn co出来: svn://netflint.net/xmpphp

简单说明:
XMPPHP is the successor to Class.Jabber.PHP that I've been promising for years. Taking advantage of PHP5, I believe it to be an elegant solution with a direct approach.
Some of the features include:

Connect to any XMPP 1.0 server (Google Talk, LJ Talk, jabber.org, etc)
Supports TLS encryption
Several XML processing approaches and supported styles (process indefinitely, processUntil an event, processTime for a number of seconds), waiting on events or map them, etc.
2.使用方法
解压。其中./XMPPHP为所需文件。其它为例子

3.示例代码
  1. <?php
  2.     require('XMPPHP/XMPP.php');  

  3.     $conn = new XMPPHP_XMPP('talk.google.com', 5222, '[email protected]',
  4.     'passwd', 'xmpphp', 'gmail.com', $printlog = true,
  5.     $loglevel = XMPPHP_Log::LEVEL_INFO);  

  6.     try {
  7.         $conn->connect();
  8.         $conn->processUntil('session_start');
  9.         $conn->presence();
  10.         $conn->message('[email protected]', 'Hi! This is a
  11.                          test message for PHP API of Gtalk~');
  12.         //sb is short for somebody, to whom you want to send  ,
  13.         // a joke!哈哈,就是SB
  14.         $conn->disconnect();
  15.     } catch(XMPPHP_Exception $e) {
  16.         die($e->getMessage());  //异常
  17.     }
  18. ?>
复制代码
4.返回消息
其中,*是用来对个人信息替换的。

1228*****
[INFO]: Connecting to tcp://talk.google.com:5222 1228****
[INFO]: Starting TLS encryption 1228****
[INFO]: Attempting Auth... 1228****
[INFO]: Auth success! 1228****
[INFO]: Bound to [email protected]/xmpphp*** 1228****
[INFO]: Session started ok

作者: 听老歌   发布时间: 2011-07-26

感谢楼主,留个记号备用。

作者: lsstarboy   发布时间: 2011-07-26