Laravel ——GuzzleHttp

Guzzle

Guzzle 是一个 PHP HTTP 客户端,致力于让发送 HTTP 请求以及与 Web 服务进行交互变得简单。
Github:https://github.com/guzzle/guzzle
Composer:https://packagist.org/packages/guzzlehttp/guzzle

简单发送请求

use GuzzleHttp\Client;
 
$client = new Client([
    //根域名
    'base_uri' => 'http://localhost',
    // 超时
    'timeout'  => 2.0,
]);
 
$response = $client->get('/get'); //请求地址:http://localhost/get,请求方式为GET
$response = $client->post('/post');//请求地址:http://localhost/post,请求方式为POST
//暂时使用较多的就是这两种 更多的还未尝试 记录下  后续补充

POST 常规 发送请求

$response = $client->request('POST', 'http://localhost/post', [
    'form_params' => [
        'username' => '用户姓名',
        'password' => '用户密码',
    ]
]);

POST JSON 发送请求

$response = $client->post('/set-username-data', [
            'headers' => [//设置 header 为json
            	'Content-Type' => 'application/json'
           	],
            'json' => [
                    'network_id' => 1,
                    'status' => 1,
                    'username' => 'aaa',
                    'password' => 'aaa'
            ]
        ]);

POST 文件 发送请求

$response = $client->post('set-file-data', [
			'headers' => [//自定义 header 头
                'Connection' => 'keep-alive',
                'Content-Type' => 'application/json',
                'Accept-Encoding' => 'gzip',
                'Accept-Charset' => 'utf-8',
            ],
            'multipart' => [//发送文件的数据 $filepath 为文件路径  fopen()函数为读取文件内容
                [
                    'name'     => 'binFile',
                    'contents' => fopen($filepath, 'r'),
                    'filename' => 'custom_filename.txt'//可选
                ],
            ],
        ]);

POST Cookies 请求

$client = new \GuzzleHttp\Client();
$url = 'https://www.baidu.com/getUserInfo';
$jar = new \GuzzleHttp\Cookie\CookieJar();
$cookie_domain = 'www.baidu.com';
$cookies = [
    'BAIDUID'   => '3XXXXXXXXXXXXXXXX',
];
$cookieJar = $jar->fromArray($cookies, $cookie_domain);
$res = $client->request('GET', $url, [
       'cookies' => $cookieJar,
       // 'debug' => true,
]);
$contents = $res->getBody()->getContents();//获取返回正文

响应

//1、获取返回正文,返回数据格式为字符串 
//如返回为utf-8格式的 需要使用utf8_decode()函数 转译,以防乱码 
//可以使用json_decode转成对象 用来其他业务上的判断
$contents = $response->getBody()->getContents();

//2、获取响应状态码 两种方式
$code = $response->getStatusCode(); // 200
$code = $response->getReasonPhrase(); //200

//3、判断header是否存在
$existence = $response->hasHeader('Content-Type');//返回 0(不存在) or 1(存在)

//4、获取某个header
$header = $response->getHeader('Content-Type');//返回格式为数组

//5、获取所有headers
$headers = $response->getHeaders();//获取全部的header

更多内容请查看手册
文章参考:Laravel ——GuzzleHttp 使用方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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 发送邮件的配置就完成了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值