ucenter與其它應用結合時出現通訊失敗,ucenter應用原理與除錯

大東瓜123發表於2015-02-07

ucenter與其它應用結合時出現通訊失敗
ucenter是一個使用者中心,多個應用可以使用ucenter,這樣使用者就有了通行證,不用一次一次地去註冊不同的使用者名稱了
現在的網際網路的使用者中心差不多就是騰訊的使用者中心了,因為大家都有QQ號

但是自己做應用的時候如何加入ucenter這樣的文章網上好多
自定義的應用,在加入應用列表後,ucenter會檢測是否與應用通訊正常,有時會失敗,
他的原理是去一個網址檢測他的返回值,地址一般是這樣的
 testlink=”admin.php?m=app&a=ping&inajax=1&url=http%3A%2F%2F192.168.0.89%3A5635&ip=&appid=3&random=14230″
回應這個連結的function 是/admin/app.php/中的onping這個函式
這個函式的作用是去訪問你在配置應用時加入的引數,一般情況下這個地方的的網址是
 http://192.168.0.89:5619/api/uc.php?code=28b4P9649Mub1wWGejarHl214enhA2Xt7vLkJPBskk50AwqDza8Ehx0T5%2FNwGtsBdB%2BmZ%2FTCibQ
 這個code是一個加密的字串,
 到了api/uc.php中會對code進行解碼,解碼出來大體上就是這樣的字串
 action=test&time=1321131313

 包含兩個引數,呼叫的函式與呼叫的時間,這樣只返回值是1就可以了,就可以通訊成功了

$get = $post = array();
$code = strval(@$_GET[`code`]); 
parse_str(uc_api_x_authcode($code, `DECODE`, UC_KEY), $get);
$timestamp = time();
if (empty($get))
{
    die(`Invalid Request`);
} elseif ($timestamp - $get[`time`] > 3600)
{
     die(`Authracation has expiried`);
}
$action = $get[`action`];
$post = xml_unserialize(file_get_contents(`php://input`));
 
if (in_array($get[`action`], array(
    `test`,
    `renameuser`,
    `synlogin`,
    `synlogout`,
    `updatepw`,
))
)
{
    $funcname = "uc_api_{$get[`action`]}";    
    if (function_exists($funcname))
    {
        exit($funcname($get, $post));
    }
}
exit(API_RETURN_FAILED);

/* communicate with ucenter */
function uc_api_test($get, $post) {
    return API_RETURN_SUCCEED;
}
/**/

看一下服務端做了哪些操作,在每次開啟應用列表時,遍歷應用並嚮應用發onping測試,函式在

Ucenter中control/admin/app.php中的onping()函式

		if($app[`extra`][`apppath`] && @include $app[`extra`][`apppath`].`./api/`.$app[`apifilename`]) {

			$uc_note = new uc_note();
			$status = $uc_note->test($note[`getdata`], $note[`postdata`]);
		} else {

			$this->load(`note`);
			$url = $_ENV[`note`]->get_url_code(`test`, ``, $appid);
			//die($url);
			//var_dump($_ENV[`app`]);die();
			$status = $_ENV[`app`]->test_api($url, $ip);
		}

如果呼叫返回結果為1那些顯示成功,否則顯示失敗,test_api在model/app.php檔案中

	function test_api($url, $ip = ``) {
		$this->base->load(`misc`);
		if(!$ip) {
			$ip = $_ENV[`misc`]->get_host_by_url($url);
		}

		if($ip < 0) {
			return FALSE;
		}
		return $_ENV[`misc`]->dfopen($url, 0, ``, ``, 1, $ip);
	}

去呼叫misc.php中的dfopen函式
dfopen函式使用php的底層函式fsockopen去與程式通訊,將通訊的結果返回onping函式,如果為1那麼顯示通訊成功,否則顯示失敗

為了方便除錯我們可以在dfopen中資訊,將輸入及輸出的值記錄到檔案中,

error_log("[uc_server]
url: $url
post: $post

", 3, `c:/log/php_fopen.txt`);


相關文章