> php教程 > php手册 > 본문

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

WBOY
풀어 주다: 2016-06-12 20:08:08
원래의
1067명이 탐색했습니다.

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으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿