注册

这是什么问题啊??求助


( ! ) Warning: curl_setopt(): You must pass either an object or an array with the CURLOPT_HTTPHEADER argument in D:\Software\wamp\www\easemob.php on line _365_
Call Stack
#TimeMemoryFunctionLocation
10.0003240352{main}( )..\testIM.php**:**0
20.0010331712Easemob->accreditRegister( )..\testIM.php**:**6
30.0010331952Easemob->getToken( )..\easemob.php**:**52
40.0010332816Easemob->postCurl( )..\easemob.php**:**342
50.0010334304http://www.php.net/function.curl-setopt ( )..\easemob.php**:**365


( ! ) Fatal error: Cannot use object of type stdClass as array in D:\Software\wamp\www\easemob.php on line _344_
Call Stack
#TimeMemoryFunctionLocation
10.0003240352{main}( )..\testIM.php**:**0
20.0010331712Easemob->accreditRegister( )..\testIM.php**:**6
30.0010331952Easemob->getToken( )..\easemob.php**:**52

已邀请:

wxw121212 - 发展中程序员

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 );
}

/**
* CURL Post
*/
private function postCurl($url, $option, $header = 0, $type = 'POST') {
$curl = curl_init (); // 启动一个CURL会话
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); // 对认证证书来源的检查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE ); // 从证书中检查SSL加密算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器
if (! empty ( $option )) {
$options = json_encode ( $option );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $options ); // Post提交的数据包
}
curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 设置超时限制防止死循环
curl_setopt ( $curl, CURLOPT_HTTPHEADER, $header ); // 设置HTTP头
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
curl_setopt ( $curl, CURLOPT_CUSTOMREQUEST, $type );
$result = curl_exec ( $curl ); // 执行操作
curl_close ( $curl ); // 关闭CURL会话
return $result;
}
直接用的我们git上面的php版本获取token吗?那个估计有点问题

wxw121212 - 发展中程序员

> forum.php?mod=redirect&goto=findpost&pid=2737&ptid=1206
直接用的我们git上面的php版本获取token吗?那个估计有点问题


你好,你是怎么处理的啊?帮忙下,谢谢
/**
* 获取Token
*/
public function getToken() {
$option = "client_credentials";
$option = $this->client_id;
$option = $this->client_secret;
$url = $this->url . "token";
$token_cache_file=$this->token_cache_file;//缓存位置

if (file_exists($token_cache_file)==FALSE){//检查文件是否存在,若不存在则创建
$path = dirname($token_cache_file);//去掉文件名,获得路径
$path = str_replace('', '/', $path);//去掉空格
$dirArray = explode('/', $path);//分割路径
$tempDir = '';
foreach ($dirArray as $dir) {
$tempDir .= $dir . '/';
if (!file_exists($tempDir)) {
mkdir($tempDir);//创建目录
}
}
touch($token_cache_file);//设置文件的访问和修改时间,如果文件不存在会自动创建
}
$file = @fopen ( $token_cache_file, 'r+' );//读写方式打开
if ($file) {//打开成功
$jsonObj = json_decode ( fgets ( $file ) );//修改序列化为json_decode
if (@$jsonObj->expires_in < time ()) {//若已经超出了缓冲时间,则重新获取token
$result = json_decode($this->postCurl ( $url, $option, $head = 0 ));
$result->expires_in = $result->expires_in + time ();
@fwrite ( $file, json_encode ( $result ) );
return $result->access_token;
fclose ( $file );
exit ();
}
return $jsonObj ->access_token;
fclose ( $file );
exit ();
}
//打开失败
fclose ( $file );
//直接返回数据
$result = json_decode($this->postCurl ( $url, $option, $head = 0 ));
$result->expires_in = $result->expires_in + time ();
return $result->access_token;
}
/**
* CURL Post
*/
private function postCurl($url, $option, $header = 0, $type = 'POST') {
$curl = curl_init (); // 启动一个CURL会话
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); // 对认证证书来源的检查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE ); // 从证书中检查SSL加密算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器
if (! empty ( $option )) {
$options = json_encode ( $option );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $options ); // Post提交的数据包
}
curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 设置超时限制防止死循环
@curl_setopt ( $curl, CURLOPT_HTTPHEADER, $header ); // 设置HTTP头
//print_r($header);
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
curl_setopt ( $curl, CURLOPT_CUSTOMREQUEST, $type );
$result = curl_exec ( $curl ); // 执行操作
//$res = object_array ( json_decode ( $result ) );
//$res = curl_getinfo ( $curl, CURLINFO_HTTP_CODE );
//pre ( $res );
curl_close ( $curl ); // 关闭CURL会话
return $result;
}
}

wxw121212 - 发展中程序员

> forum.php?mod=redirect&goto=findpost&pid=2741&ptid=1206
/**
* CURL Post
*/


public function accreditRegister($options) {
$url = $this->url . "users";
$access_token = $this->getToken ();
$header [] = 'Authorization: Bearer ' . $access_token;
$result = $this->postCurl ( $url, $options, $header );
return $result;
}
/**
* 获取Token
*/
public function getToken() {
$option = "client_credentials";
$option = $this->client_id;
$option = $this->client_secret;
$url = $this->url . "token";
/* $token_cache_file=$this->token_cache_file;//缓存位置

if (file_exists($token_cache_file)==FALSE){//检查文件是否存在,若不存在则创建
$path = dirname($token_cache_file);//去掉文件名,获得路径
$path = str_replace('', '/', $path);//去掉空格
$dirArray = explode('/', $path);//分割路径
$tempDir = '';
foreach ($dirArray as $dir) {
$tempDir .= $dir . '/';
if (!file_exists($tempDir)) {
mkdir($tempDir);//创建目录
}
}
touch($token_cache_file);//设置文件的访问和修改时间,如果文件不存在会自动创建
}
$file = @fopen ( $token_cache_file, 'r+' );//读写方式打开
if ($file) {//打开成功
$jsonObj = json_decode ( fgets ( $file ) );//修改序列化为json_decode
if (@$jsonObj->expires_in < time ()) {//若已经超出了缓冲时间,则重新获取token
$result = json_decode($this->postCurl ( $url, $option, $head = 0 ));
$result->expires_in = $result->expires_in + time ();
@fwrite ( $file, json_encode ( $result ) );
return $result->access_token;
fclose ( $file );
exit ();
}
return $jsonObj ->access_token;
fclose ( $file );
exit ();
}
//打开失败
fclose ( $file ); */
//直接返回数据
$result = json_decode($this->postCurl ( $url, $option));
//$result->expires_in = $result->expires_in + time ();
return $result->access_token;
}

/**
* CURL Post
*/
private function postCurl($url, $option, $header = 0, $type = 'POST') {
$curl = curl_init (); // 启动一个CURL会话
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); // 对认证证书来源的检查
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE ); // 从证书中检查SSL加密算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器
if (! empty ( $option )) {
$options = json_encode ( $option );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $options ); // Post提交的数据包
}
curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 设置超时限制防止死循环
@curl_setopt ( $curl, CURLOPT_HTTPHEADER, $header ); // 设置HTTP头
//print_r($header);
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
curl_setopt ( $curl, CURLOPT_CUSTOMREQUEST, $type );
$result = curl_exec ( $curl ); // 执行操作
//$res = object_array ( json_decode ( $result ) );
//$res = curl_getinfo ( $curl, CURLINFO_HTTP_CODE );
//pre ( $res );
curl_close ( $curl ); // 关闭CURL会话
return $result;
}


这样处理为什么还是有错 啊???求助呜呜

( ! ) Notice: Undefined property: stdClass::$access_token in D:\Software\wamp\www\easemob.php on line 388

Call Stack
1 0.0003 240352 {main}( ) ..\testIM.php:0
2 0.0014 329248 Easemob->accreditRegister( ) ..\testIM.php:6
3 0.0014 329464 Easemob->getToken( ) ..\easemob.php:340
交流交流好啊!楼主真厉害











static/image/common/sigline.gif
90%打工小伙一生都不可能知道的秘密http://user.qzone.qq.com/82175487

lizg - ……

获取token的问题解决了么?

lizg - ……

获取token的问题解决了么?

要回复问题请先登录注册