123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <?php
- /**
- * PHP SDK for YSDK V1.0.0
- */
- namespace huoMpay;
- use think\Log;
- /**
- * 如果您的 PHP 没有安装 cURL 扩展,请先安装
- */
- if (!function_exists('curl_init')) {
- throw new \Exception('OpenAPI needs the cURL PHP extension.');
- }
- /**
- * 如果您的 PHP 不支持JSON,请升级到 PHP 5.2.x 以上版本
- */
- if (!function_exists('json_decode')) {
- throw new \Exception('OpenAPI needs the JSON PHP extension.');
- }
- /**
- * 错误码定义
- */
- define('OPENAPI_ERROR_REQUIRED_PARAMETER_EMPTY', 1801); // 参数为空
- define('OPENAPI_ERROR_REQUIRED_PARAMETER_INVALID', 1802); // 参数格式错误
- define('OPENAPI_ERROR_RESPONSE_DATA_INVALID', 1803); // 返回包格式错误
- define('OPENAPI_ERROR_CURL', 1900); // 网络错误, 偏移量1900, 详见 http://curl.haxx.se/libcurl/c/libcurl-errors.html
- /**
- * 提供访问腾讯开放平台 YSDK 的接口
- */
- class Api {
- private $appid = 0;
- private $appkey = '';
- private $pay_appid = 0;
- private $pay_appkey = '';
- private $server_name = '';
- private $format = 'json';
- private $sdk_version = 'PHP YSDK v1.0.0';
- private $stat_url = "apistat.tencentyun.com";
- private $is_stat = true;
- /**
- * 构造函数
- *
- * @param int $appid 应用的ID
- * @param string $appkey 应用的密钥
- */
- function __construct($appid, $appkey) {
- $this->appid = $appid;
- $this->appkey = $appkey;
- }
- public function setPay($pay_appid, $pay_appkey) {
- $this->pay_appid = $pay_appid;
- $this->pay_appkey = $pay_appkey;
- }
- public function setServerName($server_name) {
- $this->server_name = $server_name;
- }
- /**
- * @return int
- */
- public function getAppid() {
- return $this->appid;
- }
- /**
- * @param int $appid
- */
- public function setAppid($appid) {
- $this->appid = $appid;
- }
- /**
- * @return string
- */
- public function getAppkey() {
- return $this->appkey;
- }
- /**
- * @param string $appkey
- */
- public function setAppkey($appkey) {
- $this->appkey = $appkey;
- }
- /**
- * @return int
- */
- public function getPayAppid() {
- return $this->pay_appid;
- }
- /**
- * @param int $pay_appid
- */
- public function setPayAppid($pay_appid) {
- $this->pay_appid = $pay_appid;
- }
- /**
- * @return string
- */
- public function getPayAppkey() {
- return $this->pay_appkey;
- }
- /**
- * @param string $pay_appkey
- */
- public function setPayAppkey($pay_appkey) {
- $this->pay_appkey = $pay_appkey;
- }
- /**
- * @return string
- */
- public function getFormat() {
- return $this->format;
- }
- /**
- * @param string $format
- */
- public function setFormat($format) {
- $this->format = $format;
- }
- /**
- * @return string
- */
- public function getSdkVersion() {
- return $this->sdk_version;
- }
- /**
- * @param string $sdk_version
- */
- public function setSdkVersion($sdk_version) {
- $this->sdk_version = $sdk_version;
- }
- public function setStatUrl($stat_url) {
- $this->stat_url = $stat_url;
- }
- public function setIsStat($is_stat) {
- $this->is_stat = $is_stat;
- }
- /**
- * 执行API调用,返回结果数组
- *
- * @param string $script_name 调用的API方法,比如/auth/verify_login,
- * 参考 http://wiki.dev.4g.qq.com/v2/ZH_CN/router/index.html#!qq.md#2.1 Oauth服务
- * @param array $params 调用API时带的参数
- * @param string $method 请求方法 post
- * @param string $protocol 协议类型 http / https
- *
- * @return array 结果数组
- */
- public function api_ysdk($script_name, $params, $method = 'post', $protocol = 'http') {
- // add some params: 'version'
- $params['version'] = $this->sdk_version;
- $url = $protocol.'://'.$this->server_name.$script_name;
- // 通过调用以下方法,可以打印出最终发送到YSDK API服务器的请求参数以及url,默认为注释
- self::printRequest($url, $params, $method);
- $cookie = array();
- // 发起请求
- $ret = SnsNetwork::makeRequest($url, $params, $cookie, $method, $protocol);
- if (false === $ret['result']) {
- $result_array = array(
- 'ret' => OPENAPI_ERROR_CURL + $ret['errno'],
- 'msg' => $ret['msg'],
- );
- } else {
- $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']
- );
- }
- }
- // 通过调用以下方法,可以打印出调用openapi请求的返回码以及错误信息,默认注释
- self::printRespond($result_array);
- return $result_array;
- }
- /**
- * @param $script_name
- * @param $accout_type
- * @param $params
- * @param string $method
- * @param string $protocol
- *
- * @return array|mixed
- */
- public function api_pay($script_name, $accout_type, $params, $method = 'post', $protocol = 'http') {
- // 添加一些参数
- $params['appid'] = $this->pay_appid;
- $params['format'] = $this->format;
- $cookie = array();
- $cookie["org_loc"] = urlencode($script_name);
- if ($accout_type == "qq") {
- $cookie["session_id"] = "openid";
- $cookie["session_type"] = "kp_actoken";
- } else if ($accout_type == "wx") {
- $cookie["session_id"] = "hy_gameid";
- $cookie["session_type"] = "wc_actoken";
- } else {
- $result_array = array(
- 'ret' => OPENAPI_ERROR_REQUIRED_PARAMETER_INVALID,
- 'msg' => '参数格式错误',
- );
- return $result_array;
- }
- // 无需传sig, 会自动生成
- unset($params['sig']);
- // 生成签名
- $secret = $this->pay_appkey.'&';
- $script_sig_name = "/v3/r".$script_name;
- $sig = SnsSigCheck::makeSig($method, $script_sig_name, $params, $secret);
- $params['sig'] = $sig;
- $url = $protocol.'://'.$this->server_name.$script_name;
- // 通过调用以下方法,可以打印出最终发送到openapi服务器的请求参数以及url,默认为注释
- self::printCookies($cookie);
- 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'],
- );
- return $result_array;
- }
- $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']
- );
- }
- // 通过调用以下方法,可以打印出调用支付API请求的返回码以及错误信息,默认注释
- self::printRespond($result_array);
- return $result_array;
- }
- /**
- * 打印出请求串的内容,当API中的这个函数的注释放开将会被调用。
- *
- * @param string $url 请求串内容
- * @param array $params 请求串的参数,必须是array
- * @param string $method 请求的方法 get / post
- */
- private function printRequest($url, $params, $method) {
- $query_string = SnsNetwork::makeQueryString($params);
- if ($method == 'get') {
- $url = $url."?".$query_string;
- }
- Log::write(
- "\n============= request info ================\n\n method:$method\n url:$url\n query_string:$query_string\n params:"
- .json_encode($params), 'error'
- );
- }
- /**
- * 打印出请求的cookies,当API中的这个函数的注释放开将会被调用。
- *
- * @param array $cookies 待打印的cookies
- */
- private function printCookies($cookies) {
- Log::write("\n============= printCookies ================\n\n Cookies: ".json_encode($cookies), 'error');
- }
- /**
- * 打印出返回结果的内容,当API中的这个函数的注释放开将会被调用。
- *
- * @param array $array 待打印的array
- */
- private function printRespond($array) {
- Log::write("\n============= printRespond ================\n\n Respond: ".json_encode($array), 'error');
- }
- /**
- * 检查 openid 的格式
- *
- * @param string $openid openid
- *
- * @return bool (true|false)
- */
- private static function isOpenId($openid) {
- return (0 == preg_match('/^[0-9a-fA-F]{32}$/', $openid)) ? false : true;
- }
- /**
- * 执行上传文件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'],
- );
- return $result_array;
- }
- $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;
- }
- /**
- * 执行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');
- }
- // 无需传sig, 会自动生成
- 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;
- self::printRequest($url, $params, $method);
- $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'],
- );
- return $result_array;
- }
- $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' => $method,
- );
- SnsStat::statReport($this->stat_url, $start_time, $stat_params);
- }
- //通过调用以下方法,可以打印出调用openapi请求的返回码以及错误信息,默认注释
- //self::printRespond($result_array);
- return $result_array;
- }
- }
- // end of script
|