client_id; $option = $this->clie..." />
注册

php reset api授权注册获取Token的问题


/**
* 获取Token
*/
public function getToken() {
$option = "client_credentials";
$option = $this->client_id;
$option = $this->client_secret;
$url = $this->url . "token";
$fp = @fopen ( "easemob.txt", 'r' );
if ($fp) {
$arr = unserialize ( fgets ( $fp ) );
if ($arr < time ()) {
$result = $this->postCurl ( $url, $option, $head = 0 );
$result = $result + time ();
@fwrite ( $fp, serialize ( $result ) );
return $result ;
fclose ( $fp );
exit ();
}
return $arr ;
fclose ( $fp );
exit ();
}
$result = $this->postCurl ( $url, $option, $head = 0 );
$result = json_decode($result);
$result = $result + time ();
$fp = @fopen ( "easemob.txt", 'w' );
@fwrite ( $fp, serialize ( $result ) );
return $result ;
fclose ( $fp );
}
这是在github上的开源代码,这里他打开了本地文件,我们应该怎么处理啊?或者说怎么实现获取Token???求助
 


已邀请:

lizg - ……

http://www.easemob.com/docs/rest/userapi/#getadmintoken 你看看,这个文档,自己获取一下。如果不行,贴下错误提示

wxw121212 - 发展中程序员

> forum.php?mod=redirect&goto=findpost&pid=2698&ptid=1176
http://www.easemob.com/docs/rest/userapi/#getadmintoken 你看看,这个文档,自己获取一下。如果不行, ...


就是不知道具体怎么用代码去实现,php
获取token,可以参考一下下方代码,还有问题的话,可在官网找我们的在线支持。/**
*获取app管理员token     
*/
function getToken()
{
    $url="https://a1.easemob.com/dihon/loveofgod/token";
    $body=array(
    "grant_type"=>"client_credentials",
    "client_id"=>"YXA6pkUGANa6EeSUKBtg-ak7UQw",
    "client_secret"=>"YXA6ZzJ5AzFGPAgYVuYFTtJs_bZnmNI2"
    );
    $patoken=json_encode($body);
    $res = postCurl($url,$patoken);
    $tokenResult = array();
    
    $tokenResult =  json_decode($res, true);
    //var_dump($tokenResult);
    return "Authorization:Bearer ". $tokenResult["access_token"];    
}
//postCurl方法
function postCurl($url, $body, $header = array(), $method = "POST")
{
    array_push($header, 'Accept:application/json');
    array_push($header, 'Content-Type:application/json');
    array_push($header, 'http:multipart/form-data');

    $ch = curl_init();//启动一个curl会话
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($ch, $method, 1);
    
    switch ($method){ 
        case "GET" : 
            curl_setopt($ch, CURLOPT_HTTPGET, true);
        break; 
        case "POST": 
            curl_setopt($ch, CURLOPT_POST,true); 
        break; 
        case "PUT" : 
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
        break; 
        case "DELETE":
            curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
        break; 
    }
    
    curl_setopt($ch, CURLOPT_USERAGENT, 'SSTS Browser/1.0');
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
    if (isset($body{3}) > 0) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    }
    if (count($header) > 0) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }

    $ret = curl_exec($ch);
    $err = curl_error($ch);
    

    curl_close($ch);
    //clear_object($ch);
    //clear_object($body);
    //clear_object($header);

    if ($err) {
        return $err;
    }

    return $ret;
}

要回复问题请先登录注册