// Authorization: <schema> <token> $url_parts = parse_url($url); $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : "")); $message = $http_method."\n". $canonical_url."\n". $timestamp."\n". $nonce."\n". $body."\n"; openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption'); $sign = base64_encode($raw_sign); $schema = 'WECHATPAY2-SHA256-RSA2048'; $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $merchant_id, $nonce, $timestamp, $serial_no, $sign);
时间: 2024-04-01 11:37:16 浏览: 65
这段代码看起来像是一个使用OpenSSL对HTTP请求进行签名的过程,其中包括了HTTP请求方法、请求路径、时间戳、随机字符串、请求体等信息,以及商户的私钥和证书序列号等参数。这个过程可能是为了保证请求的安全性和可靠性,防止请求被篡改或伪造。这是针对微信支付的,有关微信支付更多的信息可以参考微信支付官方文档。
相关问题
public function getAudiencePackage($res){ $url = 'https://ad.oceanengine.com/open_api/2/audience_package/get/'; $filtering = [ 'landing_type'=>$res['landing_type'], 'delivery_range'=>$res['delivery_range'], // 'landing_type'=>$request->landing_type, // 'delivery_range'=>$request->delivery_range, 'ad_type'=>'ALL' ]; $data = [ 'advertiser_id'=>1748895409387534, 'page'=>$res['page'], 'page_size'=>intval($res['page_size']), 'filtering'=>json_decode(json_encode($filtering)), ]; foreach ($data as $k=>$v){ if(!is_string($v)){ $data[$k] = json_encode($v); } } $request_url = $url.'?'.http_build_query($data); $token = '01c0dd4bef2fc5a2c38eac3f07eb1915bb338a7f'; $list = $this->getClient($request_url,$token); $list = json_decode($list,true); return $list; }这段代码怎么改成使用goframe2.4.3框架重写?goframe2.4.3框架使用了"github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/gclient" "github.com/gogf/gf/v2/net/ghttp"扩展,请详细一点
在GoFrame 2.4.3框架中,你可以重写代码如下:
```go
package controller
import (
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
type AudienceController struct{}
func (c *AudienceController) GetAudiencePackage(r *ghttp.Request) {
url := "https://ad.oceanengine.com/open_api/2/audience_package/get/"
filtering := g.Map{
"landing_type": r.Get("landing_type"),
"delivery_range": r.Get("delivery_range"),
"ad_type": "ALL",
}
data := g.Map{
"advertiser_id": 1748895409387534,
"page": r.Get("page"),
"page_size": r.GetInt("page_size"),
"filtering": filtering,
}
for k, v := range data {
if !gconv.IsString(v) {
data[k] = gjson.New(v).MustToJsonString()
}
}
requestURL := url + "?" + ghttp.BuildQuery(data)
token := "01c0dd4bef2fc5a2c38eac3f07eb1915bb338a7f"
client := ghttp.NewClient()
response, err := client.SetHeader("Authorization", "Bearer "+token).Get(requestURL)
if err != nil {
g.Log().Error(err)
r.Response.WriteJson(g.Map{
"code": -1,
"message": "Request failed",
"data": nil,
})
return
}
list := gjson.New(response.ReadAllString()).MustToJson()
r.Response.WriteJson(g.Map{
"code": 0,
"message": "Request success",
"data": list,
})
}
```
以上代码是一个AudienceController的示例,你可以将其添加到goframe2.4.3框架的控制器中。在该示例中,我们使用`ghttp`包发送HTTP请求,`gjson`包处理JSON数据,`g`包提供了日志记录功能。
请注意,你可能需要根据实际情况调整代码中的导入语句和其他细节,以适应你的项目。
public function handle(array $arrParam = []) { $arrDeveloper = $this->getDeveloper($this->intAdvChannel); $strClientId = $arrDeveloper['client_id']; $strClientSecret = $arrDeveloper['client_secret']; $strCode = $arrParam['authorization_code'] ?? ''; $intAdvertiserRole = $arrParam['state'] ?? 2; $arrRequestData = [ 'client_id' => $strClientId, 'client_secret' => $strClientSecret, 'grant_type' => 'authorization_code', 'authorization_code' => $strCode, 'redirect_uri' => sprintf($this->getAuthCallbackUri(), Enum::GDT), //传入的地址需要与获取 authorization_code 时,传入的回调地址保持一致 ]; $arrOption = [ RequestOptions::QUERY => $arrRequestData, 'method' => 'GET', ]; $arrSelfResponse = Utils::doRequest($this->strAccessTokenUrl, $arrOption); if ($arrSelfResponse['code'] != 200) { return $this->errorArr($arrSelfResponse['code'], $arrSelfResponse['msg']); } $arrResponse = $arrSelfResponse['data'] ?? []; if (!isset($arrResponse['code']) || $arrResponse['code'] != 0) { Log::get($this->strChannelName)->error($this->strChannelName . "-获取access_token失败", [ 'url' => $this->strAccessTokenUrl, 'request' => $arrRequestData, 'response' => $arrResponse ]); return $this->errorArr(ErrorCode::OAUTH_GET_ACCESS_TOKEN_FAIL, $arrResponse['message']); } $arrAccessToken = $arrResponse['data']; $result = $this->setOAuthToken($arrAccessToken, $intAdvertiserRole, $arrParam['auth_request_id']); if ($result) { return $this->successArr(); } return $this->errorArr(); }在goframe框架中实现以上代码
在 GoFrame 框架中,可以在某个控制器中创建一个 handle 方法,并将代码放入其中,例如:
```go
func (c *Controller) handle(r *ghttp.Request) {
arrDeveloper := c.getDeveloper(c.intAdvChannel)
strClientId := arrDeveloper["client_id"].(string)
strClientSecret := arrDeveloper["client_secret"].(string)
strCode := r.GetString("authorization_code", "")
intAdvertiserRole := r.GetInt("state", 2)
arrRequestData := g.Map{
"client_id": strClientId,
"client_secret": strClientSecret,
"grant_type": "authorization_code",
"authorization_code": strCode,
"redirect_uri": fmt.Sprintf(c.getAuthCallbackUri(), Enum.GDT),
}
arrOption := ghttp.RequestOptions{
Query: arrRequestData,
Method: "GET",
}
arrSelfResponse := Utils.doRequest(c.strAccessTokenUrl, arrOption)
if arrSelfResponse["code"].(int) != 200 {
c.jsonErrorArr(arrSelfResponse["code"].(int), arrSelfResponse["msg"].(string))
}
arrResponse := arrSelfResponse["data"].(g.Map)
if _, ok := arrResponse["code"]; !ok || arrResponse["code"].(int) != 0 {
g.Log().Error(c.strChannelName + "-获取access_token失败", g.Map{
"url": c.strAccessTokenUrl,
"request": arrRequestData,
"response": arrResponse,
})
c.jsonErrorArr(ErrorCode.OAUTH_GET_ACCESS_TOKEN_FAIL, arrResponse["message"].(string))
}
arrAccessToken := arrResponse["data"].(g.Map)
result := c.setOAuthToken(arrAccessToken, intAdvertiserRole, r.GetString("auth_request_id", ""))
if result {
c.jsonSuccessArr()
} else {
c.jsonErrorArr()
}
}
```
其中,`c.getDeveloper` 方法和 `c.getAuthCallbackUri` 方法需要根据具体情况进行实现。`c.jsonSuccessArr` 和 `c.jsonErrorArr` 方法可以在控制器中自定义实现,用于返回成功或者失败的结果。
阅读全文