Facebook チャット API、関数 xmpp_connect がブロック時に false を返す
// サーバーからチャレンジを取得し、デコードします
send_xml($fp, $AUTH_XML);
if (!find_xmpp($fp, 'CHALLENGE', null, $challenge)) {
false を返します。
}
I can't understand what is the problem. Please help.
// Copyright 2004-present Facebook. 全著作権所有。
$STREAM_XML = '';
$AUTH_XML = '';
$CLOSE_XML = '';
$RESOURCE_XML = ''.
」。
'fb_xmpp_script';
$SESSION_XML = ''.
'';
$START_TLS = '';
関数 open_connection($server) {
print "[INFO] 接続を開いています... ";
$fp = fsockopen($server, 5222, $errno, $errstr);
もし (!$fp) {
print "$errstr ($errno)
";
} そうしないと {
print "接続オープン
";
}
$fp を返します。
}
関数 send_xml($fp, $xml) {
fwrite($fp, $xml);
}
function recv_xml($fp, $size=4096) {
$xml = fread($fp, $size);
if ($xml === "") {
null を返します。
}
// xml を解析します
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $xml, $val, $index);
xml_parser_free($xml_parser);
配列を返します($val, $index);
}
function find_xmpp($fp, $tag, $value=null, &$ret=null) {
static $val = null、$index = null;
行う {
if ($val === null && $index === null) {
list($val, $index) = recv_xml($fp);
if ($val === null || $index === null) {
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]]['値'];
}
true を返します。
}
foreach ($tag_array as $i => $pos) {
if ($val[$pos]['tag'] === $tag && isset($val[$pos]['value']) &&
$val[$pos]['value'] === $value) {
$ret = $val[$pos]['value'];
true を返します。
}
}
}
}
$val = $index = null;
} ながら (!feof($fp));
false を返します。
}
function xmpp_connect($options, $access_token) {
グローバル $STREAM_XML、$AUTH_XML、$RESOURCE_XML、$SESSION_XML、$CLOSE_XML、$START_TLS;
$fp = open_connection($options['server']);
もし (!$fp) {
false を返します。
}
// 認証プロセスを開始します (X-FACEBOOK_PLATFORM を使用)
send_xml($fp, $STREAM_XML);
if (!find_xmpp($fp, 'STREAM:STREAM')) {
false を返します。
}
if (!find_xmpp($fp, 'MECHANISM', 'X-FACEBOOK-PLATFORM')) {
false を返します。
}
// tls の開始 - OAUTH トークンの使用が必須!!!!
send_xml($fp, $START_TLS);
if (!find_xmpp($fp, 'PROCEED', null, $proceed)) {
false を返します。
}
stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
send_xml($fp, $STREAM_XML);
if (!find_xmpp($fp, 'STREAM:STREAM')) {
false を返します。
}
if (!find_xmpp($fp, 'MECHANISM', 'X-FACEBOOK-PLATFORM')) {
false を返します。
}
// サーバーからチャレンジを取得し、デコードします
send_xml($fp, $AUTH_XML);
if (!find_xmpp($fp, 'CHALLENGE', null, $challenge)) {
false を返します。
}
$challenge = base64_decode($challenge);
$challenge = urldecode($challenge);
parse_str($challenge, $challenge_array);
// 応答配列を作成します
$resp_array = 配列(
'メソッド' => $challenge_array['メソッド'],
'nonce' => $challenge_array['nonce'],
'access_token' => $access_token,
'api_key' => $options['app_id'],
'call_id' => 0,
'v' => '1.0',
);
// 署名を作成します
$response = http_build_query($resp_array);
// レスポンスを送信して成功を待つ
$xml = ''.
base64_encode($response).'';
send_xml($fp, $xml);
if (!find_xmpp($fp, 'SUCCESS')) {
false を返します。
}
// 認証処理を終了
send_xml($fp, $STREAM_XML);
if (!find_xmpp($fp,'STREAM:STREAM')) {
false を返します。
}
if (!find_xmpp($fp, 'STREAM:FEATURES')) {
false を返します。
}
send_xml($fp, $RESOURCE_XML);
if (!find_xmpp($fp, 'JID')) {
false を返します。
}
send_xml($fp, $SESSION_XML);
if (!find_xmpp($fp, 'SESSION')) {
false を返します。
}
// やった!
send_xml($fp, $CLOSE_XML);
print ("認証完了
");
fclose($fp);
true を返します。
}
//xmpp_loginパーミッションでaccess_tokenを取得
function get_access_token($app_id, $app_secret, $my_url){
$code = $_REQUEST["コード"];
if(empty($code)) {
$dialog_url = "https://www.facebook.com/dialog/oauth?scope=xmpp_login".
"&client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) ;
echo("top.location.href='" . $dialog_url . "'");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&コード=" . $コード;
$access_token = file_get_contents($token_url);
parse_str($access_token, $output);
return($output['access_token']);
}
関数 _main() {
print "XMPP のテスト プラットフォーム接続
";
$app_id='';
$app_secret='';
$my_url = "";
$uid = '';
$access_token = get_access_token($app_id,$app_secret,$my_url);
print "access_token: ".$access_token."
";
$options = 配列(
'uid' => $uid,
'app_id' => $app_id,
'サーバー' => 'chat.facebook.com',
);
// 使用されたオプションを出力します
print "server: ".$options['server']."
";
print "uid: ".$options['uid']."
";
print "app id: ".$options['app_id']."
";
if (xmpp_connect($options, $access_token)) {
「完了
」を印刷します。
} そうしないと {
print "エラーが発生しました
";
}
}
_主要();