예제 #1
0
 public function api($script_name, $params, $method = 'post', $protocol = 'http')
 {
     if (!isset($params['openid']) || empty($params['openid'])) {
         return array('ret' => OPENAPI_ERROR_REQUIRED_PARAMETER_EMPTY, 'msg' => 'openid is empty');
     }
     if (!isset($params['openkey']) || empty($params['openkey'])) {
         return array('ret' => OPENAPI_ERROR_REQUIRED_PARAMETER_EMPTY, 'msg' => 'openkey is empty');
     }
     if (!self::isOpenId($params['openid'])) {
         return array('ret' => OPENAPI_ERROR_REQUIRED_PARAMETER_INVALID, 'msg' => 'openid is invalid');
     }
     unset($params['sig']);
     $params['appid'] = $this->appid;
     $params['format'] = $this->format;
     $secret = $this->appkey . '&';
     $sig = SnsSigCheck::makeSig($method, $script_name, $params, $secret);
     $params['sig'] = $sig;
     $url = $protocol . '://' . $this->server_name . $script_name;
     $cookie = array();
     $start_time = SnsStat::getTime();
     $ret = SnsNetwork::makeRequest($url, $params, $cookie, $method, $protocol);
     if (false === $ret['result']) {
         $result_array = array('ret' => OPENAPI_ERROR_CURL + $ret['errno'], 'msg' => $ret['msg']);
     }
     $result_array = json_decode($ret['msg'], true);
     if (is_null($result_array)) {
         $result_array = array('ret' => OPENAPI_ERROR_RESPONSE_DATA_INVALID, 'msg' => $ret['msg']);
     }
     if ($this->is_stat) {
         $stat_params = array('appid' => $this->appid, 'pf' => $params['pf'], 'rc' => $result_array['ret'], 'svr_name' => $this->server_name, 'interface' => $script_name, 'protocol' => $protocol, 'method' => $method);
         SnsStat::statReport($this->stat_url, $start_time, $stat_params);
     }
     return $result_array;
 }
예제 #2
0
 /**
  * 执行上传文件API调用,返回结果数组
  *
  * @param string $script_name 调用的API方法,比如/v3/user/get_info, 参考 http://wiki.open.qq.com/wiki/API_V3.0%E6%96%87%E6%A1%A3
  * @param array $params 调用API时带的参数,必须是array
  * @param array $array_files 调用API时带的文件,必须是array,key为openapi接口的参数,value为"@"加上文件全路径的字符串
  *															  举例 array('pic'=>'@/home/xxx/hello.jpg',...);
  * @param string $protocol 协议类型 http / https
  * @return array 结果数组
  */
 public function apiUploadFile($script_name, $params, $array_files, $protocol = 'http')
 {
     // 检查 openid 是否为空
     if (!isset($params['openid']) || empty($params['openid'])) {
         return array('ret' => OPENAPI_ERROR_REQUIRED_PARAMETER_EMPTY, 'msg' => 'openid is empty');
     }
     // 检查 openid 是否合法
     if (!self::isOpenId($params['openid'])) {
         return array('ret' => OPENAPI_ERROR_REQUIRED_PARAMETER_INVALID, 'msg' => 'openid is invalid');
     }
     // 无需传sig, 会自动生成
     unset($params['sig']);
     // 添加一些参数
     $params['appid'] = $this->appid;
     $params['format'] = $this->format;
     // 生成签名
     $secret = $this->appkey . '&';
     $sig = SnsSigCheck::makeSig('post', $script_name, $params, $secret);
     $params['sig'] = $sig;
     //上传文件,图片参数不能参与签名
     foreach ($array_files as $k => $v) {
         $params[$k] = $v;
     }
     $url = $protocol . '://' . $this->server_name . $script_name;
     $cookie = array();
     //记录接口调用开始时间
     $start_time = SnsStat::getTime();
     //通过调用以下方法,可以打印出最终发送到openapi服务器的请求参数以及url,默认注释
     //self::printRequest($url, $params,'post');
     // 发起请求
     $ret = SnsNetwork::makeRequestWithFile($url, $params, $cookie, $protocol);
     if (false === $ret['result']) {
         $result_array = array('ret' => OPENAPI_ERROR_CURL + $ret['errno'], 'msg' => $ret['msg']);
     }
     $result_array = json_decode($ret['msg'], true);
     // 远程返回的不是 json 格式, 说明返回包有问题
     if (is_null($result_array)) {
         $result_array = array('ret' => OPENAPI_ERROR_RESPONSE_DATA_INVALID, 'msg' => $ret['msg']);
     }
     // 统计上报
     if ($this->is_stat) {
         $stat_params = array('appid' => $this->appid, 'pf' => $params['pf'], 'rc' => $result_array['ret'], 'svr_name' => $this->server_name, 'interface' => $script_name, 'protocol' => $protocol, 'method' => 'post');
         SnsStat::statReport($this->stat_url, $start_time, $stat_params);
     }
     //通过调用以下方法,可以打印出调用openapi请求的返回码以及错误信息,默认注释
     //self::printRespond($result_array);
     return $result_array;
 }
예제 #3
0
 /**
  * 执行API调用,返回结果数组
  *
  * @param string $script_name 调用的API方法,比如/v3/user/get_info,参考 http://wiki.open.qq.com/wiki/API_V3.0%E6%96%87%E6%A1%A3
  * @param array $params 调用API时带的参数
  * @param string $method 请求方法 post / get
  * @param string $protocol 协议类型 http / https
  * @return array 结果数组
  */
 public function api($script_name, $params, $method = 'post', $protocol = 'http')
 {
     // 检查 openid 是否为空
     if (!isset($params['openid']) || empty($params['openid'])) {
         return array('ret' => OPENAPI_ERROR_REQUIRED_PARAMETER_EMPTY, 'msg' => 'openid is empty');
     }
     // 检查 openid 是否合法
     if (!self::isOpenId($params['openid'])) {
         return array('ret' => OPENAPI_ERROR_REQUIRED_PARAMETER_INVALID, 'msg' => 'openid is invalid');
     }
     // 添加一些参数
     $params['appid'] = $this->appid;
     $params['format'] = $this->format;
     $url = $protocol . '://' . $this->server_name . $script_name;
     $cookie = array();
     //记录接口调用开始时间
     $start_time = SnsStat::getTime();
     //通过调用以下方法,可以打印出最终发送到openapi服务器的请求参数以及url,默认为注释
     //self::printRequest($url,$params,$method);
     // 发起请求
     $ret = SnsNetwork::makeRequest($url, $params, $cookie, $method, $protocol);
     if (false === $ret['result']) {
         $result_array = array('ret' => OPENAPI_ERROR_CURL + $ret['errno'], 'msg' => $ret['msg']);
     }
     $result_array = json_decode($ret['msg'], true);
     // 远程返回的不是 json 格式, 说明返回包有问题
     if (is_null($result_array)) {
         $result_array = array('ret' => OPENAPI_ERROR_RESPONSE_DATA_INVALID, 'msg' => $ret['msg']);
     }
     // 统计上报
     if ($this->is_stat) {
         $stat_params = array('appid' => $this->appid, 'svr_name' => $this->server_name, 'interface' => $script_name, 'protocol' => $protocol, 'method' => $method);
         SnsStat::statReport($this->stat_url, $start_time, $stat_params);
     }
     //通过调用以下方法,可以打印出调用openapi请求的返回码以及错误信息,默认注释
     //self::printRespond($result_array);
     return $result_array;
 }