Compare commits
5 Commits
1c323ca498
...
34161fcf41
Author | SHA1 | Date |
---|---|---|
|
34161fcf41 | 7 months ago |
|
4d84d80873 | 7 months ago |
|
df945a7b4e | 7 months ago |
|
079cf71b40 | 1 year ago |
|
2c0db79cd5 | 1 year ago |
@ -0,0 +1,119 @@ |
||||
<?php |
||||
namespace app\common\service; |
||||
|
||||
class Isvapi { |
||||
private $componentAppid; |
||||
private $componentSecret; |
||||
private $domain; |
||||
|
||||
public function __construct($componentAppid, $componentSecret, $domain = "https://partner.isvapi.com/openapi") { |
||||
$this->componentAppid = $componentAppid; |
||||
$this->componentSecret = $componentSecret; |
||||
$this->domain = $domain; |
||||
} |
||||
|
||||
/** |
||||
* 查询授权应用authorizer_access_token示例 |
||||
*/ |
||||
public function getAuthorizerToken($authorizerAppid, $returnComponentToken = 1) { |
||||
$timestamp = time(); //发起请求时的时间戳(秒) |
||||
|
||||
$body = [ |
||||
'authorizer_appid' => $authorizerAppid, |
||||
'return_component_token' => $returnComponentToken |
||||
]; |
||||
|
||||
//获取签名 |
||||
$sign = $this->genSign($timestamp, $body); |
||||
$url = "{$this->domain}/wechat/get_authorizer_token?component_appid={$this->componentAppid}×tamp=$timestamp&sign=$sign"; |
||||
|
||||
//发起请求 |
||||
return $this->sendPostJson($url, $body); |
||||
// var_dump($data); |
||||
} |
||||
|
||||
/** |
||||
* 查询开放平台component_access_token示例 |
||||
*/ |
||||
public function getComponentToken() { |
||||
$timestamp = time(); //发起请求时的时间戳(秒) |
||||
|
||||
//没有请求参数时 |
||||
$body = []; |
||||
|
||||
//获取签名 |
||||
$sign = $this->genSign($timestamp, $body); |
||||
$url = "{$this->domain}/wechat/get_component_token?component_appid={$this->componentAppid}×tamp=$timestamp&sign=$sign"; |
||||
|
||||
//发起请求 |
||||
return $this->sendPostJson($url, $body); |
||||
} |
||||
|
||||
/** |
||||
* 获取授权链接 |
||||
*/ |
||||
public function getAuthUrl() { |
||||
$timestamp = time(); //发起请求时的时间戳(秒) |
||||
|
||||
//没有请求参数时 |
||||
$body = []; |
||||
|
||||
//获取签名 |
||||
$sign = $this->genSign($timestamp, $body); |
||||
$url = "{$this->domain}/wechat/get_auth_url?component_appid={$this->componentAppid}×tamp=$timestamp&sign=$sign"; |
||||
|
||||
//发起请求 |
||||
return $this->sendPostJson($url, $body); |
||||
// var_dump($data); |
||||
} |
||||
|
||||
/** |
||||
* 生成签名 |
||||
* @param int $timestamp 时间戳 |
||||
* @param array $body 请求参数 |
||||
* @return string |
||||
*/ |
||||
private function genSign(int $timestamp, array $body) { |
||||
$bodyMd5 = md5($this->genJsonBody($body)); |
||||
return md5("{$this->componentAppid},$bodyMd5,$timestamp,{$this->componentSecret}"); |
||||
} |
||||
|
||||
/** |
||||
* 生成Json格式的body |
||||
* @param array $body |
||||
* @return string |
||||
*/ |
||||
private function genJsonBody(array $body) { |
||||
return (count($body) > 0) ? json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : '{}'; |
||||
} |
||||
|
||||
/** |
||||
* 发送请求 |
||||
* @param string $url 请求地址 |
||||
* @param array $body 请求参数 |
||||
* @return array |
||||
*/ |
||||
private function sendPostJson(string $url, array $body) { |
||||
$body = $this->genJsonBody($body); |
||||
$ch = curl_init(); |
||||
curl_setopt($ch, CURLOPT_URL, $url); |
||||
curl_setopt($ch, CURLOPT_POST, 1); |
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); |
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
||||
'Content-Type: application/json; charset=utf-8', |
||||
]); |
||||
$resp = curl_exec($ch); |
||||
curl_close($ch); |
||||
|
||||
/*echo PHP_EOL; |
||||
echo "请求地址:" . $url . PHP_EOL; |
||||
echo "请求参数:" . $body . PHP_EOL; |
||||
echo "请求结果:" . $resp . PHP_EOL; |
||||
echo PHP_EOL;*/ |
||||
|
||||
return json_decode($resp, true); |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue