在Facebook的XMPP时发生错误 [英] An error ocurred in Facebook XMPP

查看:198
本文介绍了在Facebook的XMPP时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < PHP
//版权所有2004- present的Facebook。版权所有。$ STREAM_XML ='<流:流。
  '的xmlns:流=htt​​p://etherx.jabber.org/streams。
  '版本=1.0的xmlns =胡言乱语:客户端到=chat.facebook.com。
  XML:LANG =EN的xmlns:XML =http://www.w3.org/XML/1998/namespace>';$ AUTH_XML ='<权威性的xmlns =金塔:IETF:params:一个XML:NS:XMPP协议,SASL'。
  机制=X-FACEBOOK平台>< / AUTH>';$ CLOSE_XML ='< /流:流>';$ RESOURCE_XML ='<智商TYPE =设置ID =3>'。
  '<绑定的xmlns =金塔:IETF:params:一个XML:NS:XMPP协议捆绑>'。
  '<资源> fb_xmpp_script< /资源>< /绑定>< / IQ>';$ SESSION_XML ='<智商TYPE =设置ID =4=chat.facebook.com>'。
  '<会议的xmlns =金塔:IETF:params:一个XML:NS:XMPP协议会/>< / IQ>';$ START_TLS ='< STARTTLS的xmlns =金塔:IETF:params:一个XML:NS:XMPP协议,TLS/>';
功能OPEN_CONNECTION($服务器){
  打印[INFO]开幕连接...;  $计划生育=的fsockopen($服务器,5222,$的errno,$ errstr);
  如果(!$ FP){
    打印$ errstr($错误号)LT; BR>中;
  }其他{
    打印连接系统,开放式< BR>中;
  }  返回$ FP;
}功能send_xml($ FP,$ XML){
  FWRITE($ FP,$ XML);
}功能recv_xml($ FP,$大小= 4096){
  $ XML = FREAD($ FP,$大小);
  如果($ XML ===){
     返回null;
  }  //解析XML
  $ xml_parser = xml_parser_create();
  xml_parse_into_struct($ xml_parser,$ XML,$ VAL,$指数);
  xml_parser_free($ xml_parser);  返回数组($ VAL,$指数);
}功能find_xmpp($ FP,$标记,$值= NULL,&安培; $ RET = NULL){
  静态$ VAL = NULL,$指数= NULL;  做{
    如果($ VAL ===空&放大器;&安培; $指数=== NULL){
      列表($ VAL,$指数)= recv_xml($ FP);
      如果($ VAL ===空|| $指数=== NULL){
        返回false;
      }
    }    的foreach($指数$ tag_key => $ tag_array){
      如果($ tag_key === $标签){
        如果($值=== NULL){
          如果(使用isset($ VAL [$ tag_array [0] [价值])){
            $ RET = $ VAL [$ tag_array [0] ['值'];
          }
          返回true;
        }
        的foreach($ tag_array为$ I => $ POS){
          如果($ VAL [$ POS] ['标签'] === $标签和放大器;&安培;使用isset($ VAL [$ POS] ['值'])及和放大器;
            $ VAL [$ POS] ['值'] === $值){
              $ RET = $ VAL [$ POS] ['值'];
              返回true;
          }
        }
      }
    }
    $ VAL = $指数= NULL;
  }而(的feof($ FP)!);  返回false;
}
功能xmpp_connect($选项,$的access_token){
  全球$ STREAM_XML,$ AUTH_XML,$ RESOURCE_XML,$ SESSION_XML,$ CLOSE_XML,$ START_TLS;  $计划生育= OPEN_CONNECTION($选项['服务器']);
  如果(!$ FP){
    返回false;
  }  //发起身份验证过程(使用X FACEBOOK_PLATFORM)
  send_xml($ FP,$ STREAM_XML);
  如果(find_xmpp($ FP,!'STREAM:STREAM')){
    返回false;
  }
  如果(!find_xmpp($ FP,'机制','X-FACEBOOK平台')){
    返回false;
  }  //开始TLS - 强制使用OAuth凭证!!!!
  send_xml($ FP,$ START_TLS);
  如果(!find_xmpp($ FP,PROCEED,空,$继续)){
    返回false;
  }
  stream_socket_enable_crypto($ FP,真实,STREAM_CRYPTO_METHOD_TLS_C​​LIENT);  send_xml($ FP,$ STREAM_XML);
  如果(find_xmpp($ FP,!'STREAM:STREAM')){
    返回false;
  }
  如果(!find_xmpp($ FP,'机制','X-FACEBOOK平台')){
    返回false;
  }  //获得挑战来自服务器和德code将其
  send_xml($ FP,$ AUTH_XML);
  如果(!find_xmpp($ FP,'挑战',NULL,$挑战)){
    返回false;
  }
  $ =挑战base64_de code($挑战);
  $ =挑战urlde code($挑战);
  parse_str($挑战,$ challenge_array);  //创建响应数组
  $ resp_array =阵列(
    '方法'= GT; $ challenge_array ['法'],
    现时'=> $ challenge_array ['随机数'],
    ACCESS_TOKEN'=> $的access_token,
    API_KEY'=> $选项['APP_ID'],
    CALL_ID'=> 0,
    'V'=> '1.0',
  );
  //创建签名
  $响应= http_build_query($ resp_array);  //发送响应,并等待成功
  $ XML ='<响应的xmlns =金塔:IETF:params:一个XML:NS:XMPP协议,SASL>'。
    base64_en code($响应)'< /响应>'。
  send_xml($ FP,$ XML);
  如果(!find_xmpp($ FP,'成功')){
    返回false;
  }  //完成身份验证过程
  send_xml($ FP,$ STREAM_XML);
  如果(find_xmpp($ FP,!'STREAM:STREAM')){
    返回false;
  }
  如果(find_xmpp($ FP,!'STREAM:功能)){
    返回false;
  }
 send_xml($ FP,$ RESOURCE_XML);
  如果(!find_xmpp($ FP'JID')){
    返回false;
  }
  send_xml($ FP,$ SESSION_XML);
  如果(!find_xmpp($ FP,会话)){
    返回false;
  }  //我们成功了!
  send_xml($ FP,$ CLOSE_XML);
  打印(验证完整的< BR>中);
  FCLOSE($ FP);  返回true;
}//获取ACCESS_TOKEN与xmpp_login许可
功能get_access_token($ APP_ID,$ app_secret,$ my_url){  $code = $ _REQUEST [code];  如果(空($code)){
    $ dialog_url =htt​​ps://www.facebook.com/dia​​log/oauth?scope=xmpp_login。
     与& CLIENT_ID =。 $ APP_ID。与& REDIRECT_URI =。 urlen code($ my_url);
    回声(<脚本> top.location.href ='。$ dialog_url。'< / SCRIPT>中);
  }
   $ TOKEN_URL =htt​​ps://graph.facebook.com/oauth/access_token?client_id=
    。 $ APP_ID。与& REDIRECT_URI =。 urlen code($ my_url)
    。与& client_secret =。 $ app_secret
    。 &放大器; code =。 $$ C $℃;
   $ =的access_token的file_get_contents($ TOKEN_URL);
    parse_str($的access_token,$输出);    回报($输出['ACCESS_TOKEN']);
}函数_main(){
  打印测试平台连接的XMPP< BR>中;
 $ APP_ID ='';
  $ app_secret ='';
  $ my_url =;
  $ UID ='';
  $ =的access_token get_access_token($ APP_ID,$ app_secret,$ my_url);
  打印的access_token:$的access_token< BR>中;  $选项=数组(
    为'uid => $ UID,
    APP_ID'=> $ APP_ID,
    '服务器'=> chat.facebook.com',
   );  //用于打印选项
  打印服务器:$选项['服务器']< BR>中;
  。打印UID:$选项['UID']< BR>中;
  打印应用程序ID:$选项['APP_ID']< BR>中;  如果(xmpp_connect($选项,$的access_token)){
    打印完成< BR>中;
  }其他{
    打印错误途中发生< BR>中;
  }}_主要();??

我想使用Facebook的API聊天发送信息给朋友。
但上面的code给我这个作为输出,


 测试平台连接的XMPP
    的access_token:**
    服务器:chat.facebook.com
    UID:**
    应用程序ID:**
    [INFO]开幕连接...
    连接系统,开放
    时发生错误


在发现错误浪费时间后,我发现这部分返回false,

  send_xml($ FP,$ AUTH_XML);
      如果(!find_xmpp($ FP,'挑战',NULL,$挑战)){
        返回false;
      }

我还是没能找到解决的办法。
所有我想要做的就是发送消息给Facebook好友。
一个人,请帮助我通过XMPP发送了Facebook的消息。


解决方案

在功能recv_xml替换

  $ XML = FREAD($ FP,$大小);

  $ XML = FREAD($ FP,$大小); 如果(!preg_match('/ ^< /',$ XML)){
       $ XML ='<' 。 $ XML;
 }

<?php
// Copyright 2004-present Facebook. All Rights Reserved.

$STREAM_XML = '<stream:stream '.
  'xmlns:stream="http://etherx.jabber.org/streams" '.
  'version="1.0" xmlns="jabber:client" to="chat.facebook.com" '.
  'xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace">';

$AUTH_XML = '<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" '.
  'mechanism="X-FACEBOOK-PLATFORM"></auth>';

$CLOSE_XML = '</stream:stream>';

$RESOURCE_XML = '<iq type="set" id="3">'.
  '<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">'.
  '<resource>fb_xmpp_script</resource></bind></iq>';

$SESSION_XML = '<iq type="set" id="4" to="chat.facebook.com">'.
  '<session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></iq>';

$START_TLS = '<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>';


function open_connection($server) {
  print "[INFO] Opening connection... ";

  $fp = fsockopen($server, 5222, $errno, $errstr);
  if (!$fp) {
    print "$errstr ($errno)<br>";
  } else {
    print "connnection open<br>";
  }

  return $fp;
}

function send_xml($fp, $xml) {
  fwrite($fp, $xml);
}

function recv_xml($fp,  $size=4096) {
  $xml = fread($fp, $size);
  if ($xml === "") {
     return null;
  }

  // parses xml
  $xml_parser = xml_parser_create();
  xml_parse_into_struct($xml_parser, $xml, $val, $index);
  xml_parser_free($xml_parser);

  return array($val, $index);
}

function find_xmpp($fp,  $tag, $value=null, &$ret=null) {
  static $val = null, $index = null;

  do {
    if ($val === null && $index === null) {
      list($val, $index) = recv_xml($fp);
      if ($val === null || $index === null) {
        return false;
      }
    }

    foreach ($index as $tag_key => $tag_array) {
      if ($tag_key === $tag) {
        if ($value === null) {
          if (isset($val[$tag_array[0]]['value'])) {
            $ret = $val[$tag_array[0]]['value'];
          }
          return true;
        }
        foreach ($tag_array as $i => $pos) {
          if ($val[$pos]['tag'] === $tag && isset($val[$pos]['value']) &&
            $val[$pos]['value'] === $value) {
              $ret = $val[$pos]['value'];
              return true;
          }
        }
      }
    }
    $val = $index = null;
  } while (!feof($fp));

  return false;
}


function xmpp_connect($options, $access_token) {
  global $STREAM_XML, $AUTH_XML, $RESOURCE_XML, $SESSION_XML, $CLOSE_XML, $START_TLS;

  $fp = open_connection($options['server']);
  if (!$fp) {
    return false;
  }

  // initiates auth process (using X-FACEBOOK_PLATFORM)
  send_xml($fp,  $STREAM_XML);
  if (!find_xmpp($fp, 'STREAM:STREAM')) {
    return false;
  }
  if (!find_xmpp($fp,  'MECHANISM', 'X-FACEBOOK-PLATFORM')) {
    return false;
  }

  // starting tls - MANDATORY TO USE OAUTH TOKEN!!!!
  send_xml($fp,  $START_TLS);
  if (!find_xmpp($fp, 'PROCEED', null, $proceed)) {
    return false;
  }
  stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);

  send_xml($fp, $STREAM_XML);
  if (!find_xmpp($fp, 'STREAM:STREAM')) {
    return false;
  }
  if (!find_xmpp($fp, 'MECHANISM', 'X-FACEBOOK-PLATFORM')) {
    return false;
  }

  // gets challenge from server and decode it
  send_xml($fp, $AUTH_XML);
  if (!find_xmpp($fp,  'CHALLENGE', null, $challenge)) {
    return false;
  }
  $challenge = base64_decode($challenge);
  $challenge = urldecode($challenge);
  parse_str($challenge, $challenge_array);

  // creates the response array
  $resp_array = array(
    'method' => $challenge_array['method'],
    'nonce' => $challenge_array['nonce'],
    'access_token' => $access_token,
    'api_key' => $options['app_id'],
    'call_id' => 0,
    'v' => '1.0',
  );
  // creates signature
  $response = http_build_query($resp_array);

  // sends the response and waits for success
  $xml = '<response xmlns="urn:ietf:params:xml:ns:xmpp-sasl">'.
    base64_encode($response).'</response>';
  send_xml($fp, $xml);
  if (!find_xmpp($fp, 'SUCCESS')) {
    return false;
  }

  // finishes auth process
  send_xml($fp, $STREAM_XML);
  if (!find_xmpp($fp,'STREAM:STREAM')) {
    return false;
  }
  if (!find_xmpp($fp, 'STREAM:FEATURES')) {
    return false;
  }
 send_xml($fp, $RESOURCE_XML);
  if (!find_xmpp($fp, 'JID')) {
    return false;
  }
  send_xml($fp, $SESSION_XML);
  if (!find_xmpp($fp, 'SESSION')) {
    return false;
  }

  // we made it!
  send_xml($fp, $CLOSE_XML);
  print ("Authentication complete<br>");
  fclose($fp);

  return true;
}



//Gets access_token with xmpp_login permission
function get_access_token($app_id, $app_secret, $my_url){ 

  $code = $_REQUEST["code"];

  if(empty($code)) {
    $dialog_url = "https://www.facebook.com/dialog/oauth?scope=xmpp_login".
     "&client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) ;
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }
   $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&client_secret=" . $app_secret 
    . "&code=" . $code;
   $access_token = file_get_contents($token_url);
    parse_str($access_token, $output);

    return($output['access_token']);
}

function _main() {
  print "Test platform connect for XMPP<br>";
 $app_id='';
  $app_secret='';
  $my_url = "";
  $uid = '';
  $access_token = get_access_token($app_id,$app_secret,$my_url);
  print "access_token: ".$access_token."<br>";

  $options = array(
    'uid' => $uid,
    'app_id' => $app_id,
    'server' => 'chat.facebook.com',
   );

  // prints options used
  print "server: ".$options['server']."<br>";
  print "uid: ".$options['uid']."<br>";
  print "app id: ".$options['app_id']."<br>";

  if (xmpp_connect($options, $access_token)) {
    print "Done<br>";
  } else {
    print "An error ocurred<br>";
  }

}

_main();

??

I am trying to send message to friend using facebook chat API. But the above code gives me this as output,

  Test platform connect for XMPP 
    access_token: **
    server: chat.facebook.com 
    uid: ** 
    app id: **
    [INFO] Opening connection... 
    connnection open
    An error ocurred

After wasting hours on finding the error I found this part returns false,

send_xml($fp, $AUTH_XML);
      if (!find_xmpp($fp,  'CHALLENGE', null, $challenge)) {
        return false;
      }

I still couldn't find a solution. All I want to do is send message to facebook friend. Someone, please help me sending message in facebook via XMPP.

解决方案

In the function recv_xml replace

 $xml = fread($fp, $size);

to

 $xml = fread($fp, $size);

 if (!preg_match('/^</', $xml)) {
       $xml = '<' . $xml;
 }

这篇关于在Facebook的XMPP时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆