首页 > php教程 > php手册 > 正文

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

WBOY
发布: 2016-06-12 20:08:08
原创
1066 人浏览过

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.示例代码

<span style="color: #000000;"></span><span style="color: #000000;">php
    </span><span style="color: #0000ff;">require</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">XMPPHP/XMPP.php</span><span style="color: #000000;">'</span><span style="color: #000000;">);  

    </span><span style="color: #800080;">$conn</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> XMPPHP_XMPP(</span><span style="color: #000000;">'</span><span style="color: #000000;">talk.google.com</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">5222</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">user@gmail.com</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">
    </span><span style="color: #000000;">'</span><span style="color: #000000;">passwd</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">xmpphp</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">gmail.com</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$printlog</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">true</span><span style="color: #000000;">,</span><span style="color: #000000;">
    </span><span style="color: #800080;">$loglevel</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> XMPPHP_Log</span><span style="color: #000000;">::</span><span style="color: #000000;">LEVEL_INFO);  

    </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
        </span><span style="color: #800080;">$conn</span><span style="color: #000000;">-></span><span style="color: #000000;">connect();
        </span><span style="color: #800080;">$conn</span><span style="color: #000000;">-></span><span style="color: #000000;">processUntil(</span><span style="color: #000000;">'</span><span style="color: #000000;">session_start</span><span style="color: #000000;">'</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$conn</span><span style="color: #000000;">-></span><span style="color: #000000;">presence();
        </span><span style="color: #800080;">$conn</span><span style="color: #000000;">-></span><span style="color: #000000;">message(</span><span style="color: #000000;">'</span><span style="color: #000000;">sb@gmail.com</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">Hi! This is a
                         test message for PHP API of Gtalk~</span><span style="color: #000000;">'</span><span style="color: #000000;">);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">sb is short for somebody, to whom you want to send  ,
        // a joke!哈哈,就是SB</span>
<span style="color: #000000;">        </span><span style="color: #800080;">$conn</span><span style="color: #000000;">-></span><span style="color: #000000;">disconnect();
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;">(XMPPHP_Exception </span><span style="color: #800080;">$e</span><span style="color: #000000;">) {
        </span><span style="color: #0000ff;">die</span><span style="color: #000000;">(</span><span style="color: #800080;">$e</span><span style="color: #000000;">-></span><span style="color: #000000;">getMessage());  </span><span style="color: #008000;">//</span><span style="color: #008000;">异常</span>
<span style="color: #000000;">    }
</span><span style="color: #000000;">?>
</span>
登录后复制

4.返回消息

其中,*是用来对个人信息替换的。

<span style="color: #800080;">1228</span><span style="color: #000000;">*****
[INFO]: Connecting to </span><span style="color: #008080;">tcp:</span><span style="color: #000000;">//talk.google.</span><span style="color: #008080;">com:</span><span style="color: #800080;">5222</span><span style="color: #000000;"> </span><span style="color: #800080;">1228</span><span style="color: #000000;">****
[INFO]: Starting TLS encryption </span><span style="color: #800080;">1228</span><span style="color: #000000;">****
[INFO]: Attempting Auth... </span><span style="color: #800080;">1228</span><span style="color: #000000;">****
[INFO]: Auth success! </span><span style="color: #800080;">1228</span><span style="color: #000000;">****
[INFO]: </span><span style="color: #0000ff;">Bound</span><span style="color: #000000;"> to user@gmail.com/xmpphp*** </span><span style="color: #800080;">1228</span><span style="color: #000000;">****
[INFO]: Session started ok</span>
登录后复制
 » 转载保留版权:叽叽歪歪 » 《Gtalk PHP API 简单例子(基于XMPP)》
 » 本文链接地址:http://www.jijiwaiwai.info/2011/07/25/gtalk_php_api_xmpp_example/
 » 如果喜欢可以:点此订阅本站
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板