我正在使用 JAXL 3.x 编写一个模块,允许我向另一个 facebook 用户发送消息。
public function sendTo($id = null, $name = null, $user = null){
if(!$id)
throw new NotFoundException(__('Invalid user id'));
if(!$name)
throw new NotFoundException(__('Invalid name'));
if(!$user)
throw new NotFoundException(__('Invalid user'));
$this->set('mess',$_POST['mess']);
$this->set('id',$id);
$this->set('name',$name);
$config = new Config();
$client = new JAXL(array(
// (required) credentials
'jid' => $id,
'fb_app_key' => $config->fb_app_id,
'fb_access_token' => $config->fb_access_token,
// force tls (facebook require this now)
'force_tls' => true,
// (required) force facebook oauth
'auth_type' => 'X-FACEBOOK-PLATFORM',
// (optional)
//'resource' => 'resource',
'log_level' => JAXL_INFO
));
//
// add necessary event callbacks here
//
$client->add_cb('on_auth_success', function() {
echo "got on_auth_success cb, jid ".$client->full_jid->to_string();
});
$client->add_cb('on_auth_failure', function($reason) {
$client->send_end_stream();
echo "got on_auth_failure cb with reason $reason";
});
$client->add_cb('on_chat_message', function($stanza) {
// echo back incoming message stanza
$stanza->to = $id;
$stanza->from = $client->full_jid->to_string();
$client->send($stanza);
});
$client->add_cb('on_disconnect', function() {
echo "got disconnected";
});
//
// finally start configured xmpp stream
//
$client->start();
echo "done\n";
}
但在输入“on_auth_success”后,我发现一个空 $client 错误。但是正如您在代码中看到的那样,在创建 $client 之后,我尝试通过 print_r($client); 打印出它的内容;结果是:
JAXL Object ( [cfg] => Array ( [jid] => -to_some_one@chat.facebook.com [fb_app_key] => appid [fb_access_token] => access_token [force_tls] => 1 [auth_type] => X-FACEBOOK-PLATFORM [log_level] => 4 [host] => chat.facebook.com [port] => 5222 ) [ev:protected] => JAXLEvent Object ( [common:protected] => Array ( ) [reg] => Array ( ) ) [xeps] => Array ( [0030] => XEP_0030 Object ( [jaxl:protected] => JAXL Object *RECURSION* ) [0115] => XEP_0115 Object ( [jaxl:protected] => JAXL Object *RECURSION* ) ) [roster] => Array ( ) [manage_roster] => 1 [manage_subscribe] => none [log_level] => 4 [priv_dir] => C:\xampp\htdocs\cake\app\Lib\Facebook/.jaxl [tmp_dir] => C:\xampp\htdocs\cake\app\Lib\Facebook/.jaxl/tmp [log_dir] => C:\xampp\htdocs\cake\app\Lib\Facebook/.jaxl/log [pid_dir] => C:\xampp\htdocs\cake\app\Lib\Facebook/.jaxl/run [sock_dir] => C:\xampp\htdocs\cake\app\Lib\Facebook/.jaxl/sock [sock:JAXL:private] => [cli:JAXL:private] => [local_ip] => 172.17.1.137 [pid] => 4736 [mode] => apache2handler [status] => [features] => Array ( ) [category] => client [type] => bot [lang] => en [retry] => 1 [retry_interval:JAXL:private] => 1 [retry_attempt:JAXL:private] => 0 [retry_max_interval:JAXL:private] => 32 [full_jid] => [jid] => XMPPJid Object ( [node] => -ffff[domain] => chat.facebook.com [resource] => [bare] => -fffff@chat.facebook.com ) [pass] => [resource] => jaxl#f2991f0002fbd1af8df4754c1df8683c [force_tls] => 1 [trans:protected] => JAXLSocketClient Object ( [host:JAXLSocketClient:private] => [port:JAXLSocketClient:private] => [transport:JAXLSocketClient:private] => [stream_context:JAXLSocketClient:private] => [blocking:JAXLSocketClient:private] => [fd] => [errno] => [errstr] => [timeout:JAXLSocketClient:private] => 10 [ibuffer:JAXLSocketClient:private] => [obuffer:JAXLSocketClient:private] => [compressed:JAXLSocketClient:private] => [recv_bytes:JAXLSocketClient:private] => 0 [send_bytes:JAXLSocketClient:private] => 0 [recv_cb:JAXLSocketClient:private] => Array ( [0] => JAXLXmlStream Object ( [delimiter:JAXLXmlStream:private] => \ [ns:JAXLXmlStream:private] => [parser:JAXLXmlStream:private] => Resource id #93 [stanza:JAXLXmlStream:private] => [depth:JAXLXmlStream:private] => -1 [start_cb:JAXLXmlStream:private] => Array ( [0] => JAXL Object *RECURSION* [1] => start_cb ) [stanza_cb:JAXLXmlStream:private] => Array ( [0] => JAXL Object *RECURSION* [1] => stanza_cb ) [end_cb:JAXLXmlStream:private] => Array ( [0] => JAXL Object *RECURSION* [1] => end_cb ) ) [1] => parse ) [recv_chunk_size:JAXLSocketClient:private] => 1024 [writing:JAXLSocketClient:private] => ) [xml:protected] => JAXLXmlStream Object ( [delimiter:JAXLXmlStream:private] => \ [ns:JAXLXmlStream:private] => [parser:JAXLXmlStream:private] => Resource id #93 [stanza:JAXLXmlStream:private] => [depth:JAXLXmlStream:private] => -1 [start_cb:JAXLXmlStream:private] => Array ( [0] => JAXL Object *RECURSION* [1] => start_cb ) [stanza_cb:JAXLXmlStream:private] => Array ( [0] => JAXL Object *RECURSION* [1] => stanza_cb ) [end_cb:JAXLXmlStream:private] => Array ( [0] => JAXL Object *RECURSION* [1] => end_cb ) ) [last_id:protected] => 0 [state:protected] => setup )
这意味着 $client 成功连接到 XMPP 服务。有什么帮助吗?
\\\\\\\\添加评论\\\\\\\\\\
我在开发网络应用程序时使用 cakephp。