guzzle 使用 json 作为主体请求接口

在使用第三方插件 Guzzle 请求微信素材管理接口:

接口说明

http请求方式: POST
https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN

#参数
{
   
    "type":TYPE,
    "offset":OFFSET,
    "count":COUNT
}

错误请求代码

$client = new Client();
$response 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Laravel 中使用 GuzzleHttp/Guzzle 发送邮件可以通过以下步骤实现: 1. 安装 GuzzleHttp/Guzzle 可以使用 Composer 进行安装: ``` composer require guzzlehttp/guzzle ``` 2. 创建邮件发送类 在 app 目录下创建一个名为 MailSender 的类,代码如下: ```php <?php namespace App; use GuzzleHttp\Client; class MailSender { protected $client; public function __construct() { $this->client = new Client([ 'base_uri' => 'https://api.sendgrid.com/v3/', 'headers' => [ 'Authorization' => 'Bearer ' . env('SENDGRID_API_KEY'), 'Content-Type' => 'application/json' ] ]); } public function send($to, $subject, $content) { $response = $this->client->request('POST', 'mail/send', [ 'json' => [ 'personalizations' => [ [ 'to' => [ [ 'email' => $to ] ] ] ], 'from' => [ 'email' => 'sender@example.com' ], 'subject' => $subject, 'content' => [ [ 'type' => 'text/plain', 'value' => $content ] ] ] ]); return $response->getStatusCode(); } } ``` 其中,使用 GuzzleHttp\Client 创建一个 HTTP 客户端,设置 base_uri 为 SendGrid 邮件服务的 API 地址,headers 中包含 Authorization 和 Content-Type 信息。send() 方法接受收件人邮箱地址、邮件主题和邮件内容,使用 HTTP POST 请求发送邮件。 3. 在控制器中使用 MailSender 发送邮件 在需要发送邮件的控制器中,使用 MailSender 类发送邮件,示例代码如下: ```php <?php namespace App\Http\Controllers; use App\MailSender; use Illuminate\Http\Request; class MailController extends Controller { public function send(Request $request) { $to = $request->input('to'); $subject = $request->input('subject'); $content = $request->input('content'); $mailSender = new MailSender(); $statusCode = $mailSender->send($to, $subject, $content); return response()->json(['status' => $statusCode]); } } ``` 在 send() 方法中,从请求中获取收件人邮箱地址、邮件主题和邮件内容,然后实例化 MailSender 类并调用 send() 方法发送邮件。 4. 配置 SendGrid API 密钥 在 .env 文件中添加 SendGrid API 密钥: ``` SENDGRID_API_KEY=your_api_key_here ``` 至此,使用 GuzzleHttp/Guzzle 发送邮件的配置就完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值