123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <?php
- namespace huoMpay;
- use think\Log;
- if (!function_exists('curl_init')) {
- throw new \Exception('OpenAPI needs the cURL PHP extension.');
- }
- 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);
- 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;
-
- 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;
- }
-
- public function getAppid() {
- return $this->appid;
- }
-
- public function setAppid($appid) {
- $this->appid = $appid;
- }
-
- public function getAppkey() {
- return $this->appkey;
- }
-
- public function setAppkey($appkey) {
- $this->appkey = $appkey;
- }
-
- public function getPayAppid() {
- return $this->pay_appid;
- }
-
- public function setPayAppid($pay_appid) {
- $this->pay_appid = $pay_appid;
- }
-
- public function getPayAppkey() {
- return $this->pay_appkey;
- }
-
- public function setPayAppkey($pay_appkey) {
- $this->pay_appkey = $pay_appkey;
- }
-
- public function getFormat() {
- return $this->format;
- }
-
- public function setFormat($format) {
- $this->format = $format;
- }
-
- public function getSdkVersion() {
- return $this->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;
- }
-
- public function api_ysdk($script_name, $params, $method = 'post', $protocol = 'http') {
-
- $params['version'] = $this->sdk_version;
- $url = $protocol.'://'.$this->server_name.$script_name;
-
- 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);
-
- if (is_null($result_array)) {
- $result_array = array(
- 'ret' => OPENAPI_ERROR_RESPONSE_DATA_INVALID,
- 'msg' => $ret['msg']
- );
- }
- }
-
- self::printRespond($result_array);
- return $result_array;
- }
-
- 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;
- }
-
- 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;
-
- 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);
-
- if (is_null($result_array)) {
- $result_array = array(
- 'ret' => OPENAPI_ERROR_RESPONSE_DATA_INVALID,
- 'msg' => $ret['msg']
- );
- }
-
- self::printRespond($result_array);
- return $result_array;
- }
-
- 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'
- );
- }
-
- private function printCookies($cookies) {
- Log::write("\n============= printCookies ================\n\n Cookies: ".json_encode($cookies), 'error');
- }
-
- private function printRespond($array) {
- Log::write("\n============= printRespond ================\n\n Respond: ".json_encode($array), 'error');
- }
-
- private static function isOpenId($openid) {
- return (0 == preg_match('/^[0-9a-fA-F]{32}$/', $openid)) ? false : true;
- }
-
- public function apiUploadFile($script_name, $params, $array_files, $protocol = 'http') {
-
- if (!isset($params['openid']) || empty($params['openid'])) {
- return array(
- 'ret' => OPENAPI_ERROR_REQUIRED_PARAMETER_EMPTY,
- 'msg' => 'openid 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('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();
-
-
-
- $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);
-
- 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);
- }
-
-
- return $result_array;
- }
-
- 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 (!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;
- self::printRequest($url, $params, $method);
- $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'],
- );
- return $result_array;
- }
- $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;
- }
- }
|