Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-12 20:08:08
Original
1061 people have browsed it

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>
Copy after login

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>
Copy after login
 » 转载保留版权:叽叽歪歪 » 《Gtalk PHP API 简单例子(基于XMPP)》
 » 本文链接地址:http://www.jijiwaiwai.info/2011/07/25/gtalk_php_api_xmpp_example/
 » 如果喜欢可以:点此订阅本站
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!