facebook 聊天 api - php

Posted

技术标签:

【中文标题】facebook 聊天 api - php【英文标题】:facebook chat api - php 【发布时间】:2012-04-17 17:15:58 【问题描述】:

我已经成功通过 xmpp 协议登录到 facebook。

我使用了此处找到的 facebook 文档中提供的 php 代码:

<?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')) 
  echo "falss";
    return false;
  
  if (!find_xmpp($fp,  'MECHANISM', 'X-FACEBOOK-PLATFORM')) 
  echo "f4ls";
    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 = "http://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='YOUR_APP_ID';
  $app_secret='YOUR-APP_SECRET';
  $my_url = "YOUR_APP_URL";
  $uid = 'USER_ID';
  $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();

我几乎了解所有内容,但我没有看到任何用于检索在线列表、发送/接收消息的文档。

【问题讨论】:

【参考方案1】:

发送&lt;presence /&gt;上线。 然后服务器会给你发几个&lt;presence from='friend1@chat.facebook.com/deviceID' /&gt;表示在线好友。

发送这样的消息:

<message from='juliet@example.com' to='romeo@example.net'>
    <body>Art thou not Romeo, and a Montague?</body>
</message>

【讨论】:

在哪里?在调用 _main() 之后?打印后“完成”; 我在 fclose($fp); 之前的 xmpp_connect 函数中添加了我的代码--------- send_xml($fp, "&lt;presence /&gt;"); print ("go online done&lt;br&gt;\n"); send_xml($fp, "&lt;message from='my_id@chat.facebook.com' to='my_friend_id@chat.facebook.com'&gt;&lt;body&gt;55&lt;/body&gt;&lt;/message&gt;"); ----- 上网部分完成,但消息未发送!!。为什么 ??请提供任何帮助 嗨 Joakim ...我尝试了 但 facebook 花了很长时间并最终显示超时错误..虽然我能够向用户发送消息..但无法获得在线用户..请帮助 不会有响应,所以不要等待。相反,您会收到几条单独的 消息,因此您必须监听传入的消息。

以上是关于facebook 聊天 api - php的主要内容,如果未能解决你的问题,请参考以下文章

既然 Facebook 即将禁用 XMPP API,那么使用 Facebook 聊天 API 的替代方法是啥?

使用图形 api 集成 facebook 聊天

Facebook 聊天 API 会消失吗?

使用图形 API 进行 Facebook 聊天

facebook 聊天 api - php

facebook 聊天 API 登录失败