public function putAdImage($res){ $curl = curl_init(); $token = '75b824425e25e16876a5467d8e072d058ea7ba04'; curl_setopt_array($curl, array( CURLOPT_URL => 'https://ad.oceanengine.com/open_api/2/file/image/ad/', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 60, CURLOPT_TIMEOUT => 60, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_SSL_VERIFYPEER=> false, CURLOPT_POSTFIELDS => array('advertiser_id' => 1748895409387534,'upload_type' => 'UPLOAD_BY_FILE','image_signature' => md5_file($res['image_file']),'image_file'=> new CURLFILE($res['image_file']),'filename' =>$res['filename']), CURLOPT_HTTPHEADER => array( 'Content-Type: multipart/form-data', 'Access-Token:'.$token ), )); $response = curl_exec($curl); curl_close($curl); echo $response; }这段代码怎么改成使用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"扩展,请详细一点
时间: 2024-04-18 20:30:28 浏览: 355
php中使用Curl、socket、file_get_contents三种方法POST提交数据
在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 ImageController struct{}
func (c *ImageController) PutAdImage(r *ghttp.Request) {
url := "https://ad.oceanengine.com/open_api/2/file/image/ad/"
token := "75b824425e25e16876a5467d8e072d058ea7ba04"
client := ghttp.NewClient()
client.SetHeader("Content-Type", "multipart/form-data")
client.SetHeader("Access-Token", token)
uploadType := "UPLOAD_BY_FILE"
imageSignature := gfile.Md5(r.GetUploadFile("image_file"))
filename := r.Get("filename")
response, err := client.PostMultipart(url, g.Map{
"advertiser_id": 1748895409387534,
"upload_type": uploadType,
"image_signature": imageSignature,
"image_file": r.GetUploadFile("image_file"),
"filename": filename,
})
if err != nil {
g.Log().Error(err)
r.Response.WriteJson(g.Map{
"code": -1,
"message": "Request failed",
"data": nil,
})
return
}
r.Response.Write(response.ReadAll())
}
```
以上代码是一个ImageController的示例,你可以将其添加到goframe2.4.3框架的控制器中。在该示例中,我们使用`ghttp`包发送HTTP请求,`gfile`包处理文件操作,`g`包提供了日志记录功能。
请注意,你可能需要根据实际情况调整代码中的导入语句和其他细节,以适应你的项目。
阅读全文