Laravel中用GuzzleHttp
今天项目中用到GuzzleHttp,开始不知道怎么用,其实还是很简单的。
直接在项目根目录,输入以下命令
composer require guzzlehttp/guzzle
- 1
等下载安装好,在vendor文件夹下,有一个guzzle目录,此文件夹就是guzzlehttp的package了。
如何使用,可以参考官方文档http://docs.guzzlephp.org/en/latest/
下面这段代码就是官网文档中的一段
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'
// Send an asynchronous request.
$request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
$promise = $client->sendAsync($request)->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
$promise->wait();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
我在项目中,已经使用了form表单post,异步请求等等。
这篇文章还是挺有意思的《Laravel
下使用 Guzzle 编写多线程爬虫实战》,代码啥都有,虽然是个小玩意,但能学到很多东西。
比如:
- 在Laravel中如何创建命令
- 怎么用多线程
贴一下代码
<?php namespace App\Console\Commands;
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Console\Command;
class MultithreadingRequest extends Command
{
private $totalPageCount;
private $counter = 1;
private $concurrency = 7; // 同时并发抓取
private $users = ['CycloneAxe', 'appleboy', 'Aufree', 'lifesign',
'overtrue', 'zhengjinghua', 'NauxLiu'];
protected $signature = 'test:multithreading-request';
protected $description = 'Command description';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->totalPageCount = count($this->users);
$client = new Client();
$requests = function ($total) use ($client) {
foreach ($this->users as $key => $user) {
$uri = 'https://api.github.com/users/' . $user;
yield function() use ($client, $uri) {
return $client->getAsync($uri);
};
}
};
$pool = new Pool($client, $requests($this->totalPageCount), [
'concurrency' => $this->concurrency,
'fulfilled' => function ($response, $index){
$res = json_decode($response->getBody()->getContents());
$this->info("请求第 $index 个请求,用户 " . $this->users[$index] . " 的 Github ID 为:" .$res->id);
$this->countedAndCheckEnded();
},
'rejected' => function ($reason, $index){
$this->error("rejected" );
$this->error("rejected reason: " . $reason );
$this->countedAndCheckEnded();
},
]);
// 开始发送请求
$promise = $pool->promise();
$promise->wait();
}
public function countedAndCheckEnded()
{
if ($this->counter < $this->totalPageCount){
$this->counter++;
return;
}
$this->info("请求结束!");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
运行结果如下:
$ php artisan test:multithreading-request
请求第 5 个请求,用户 zhengjinghua 的 Github ID 为:3413430
请求第 6 个请求,用户 NauxLiu 的 Github ID 为:9570112
请求第 0 个请求,用户 CycloneAxe 的 Github ID 为:6268176
请求第 1 个请求,用户 appleboy 的 Github ID 为:21979
请求第 2 个请求,用户 Aufree 的 Github ID 为:5310542
请求第 3 个请求,用户 lifesign 的 Github ID 为:2189610
请求第 4 个请求,用户 overtrue 的 Github ID 为:1472352
请求结束!
Laravel中用GuzzleHttp的更多相关文章
- laravel中用到的ServiceProvide
路由 全局限制 如果你希望路由参数可以总是遵循正则表达式,则可以使用 pattern 方法.你应该在 RouteServiceProvider 的 boot 方法里定义这些模式: 1 2 3 4 5 ...
- Laravel核心解读--HTTP内核
Http Kernel Http Kernel是Laravel中用来串联框架的各个核心组件来网络请求的,简单的说只要是通过public/index.php来启动框架的都会用到Http Kernel,而 ...
- PHP 从另一个角度来分析 Laravel 框架的依赖注入功能
从根本上说,依赖注入不是让对象创建一个依赖关系,也不是让工厂对象去创建对象,而是将所需的依赖变成一个外部对象,使之成为一个"某些人的问题” 你为"某些人的问题”注入了类的依赖关系. ...
- 关于在框架中使用curl的思考,以及,curl其实很好用
初步猜想: 在接触到框架文档的第一阶段时,会觉得控制器调用模型就是一件很简单的事,tp中用D方法或者M方法来实例化模型,laravel中用命名空间来加载模型,CI中用$this->load-&g ...
- legend3---12、DB::table('user_questions')和UserQuestion查询的结果的格式不一样
legend3---12.DB::table('user_questions')和UserQuestion查询的结果的格式不一样 一.总结 一句话总结: 推荐使用模型查找的方式,可以直接数组方式访问: ...
- Mac OSX编译安装php7.1.8
laravel中用到ldap认证包,要求php7.0以上版本,而且安装Mews\Captcha包的时候 验证码无法显示 报错如下: Call to undefined function Interve ...
- Laravel 5.6 安装 guzzlehttp
环境:Laravel 5.6 安装 composer require guzzlehttp/guzzle 在vendor文件夹下,vendor\guzzlehttp\guzzle 引入 use Gu ...
- 通过中看不中用的代码分析Ioc容器,依赖注入....
/** * 通过生产拥有超能力的超人实例 来理解IOC容器 */ //超能力模组接口 interface SuperModuleInterface{ public function activate( ...
- laravel安装学习步骤
在看知乎比较php框架的优劣的时候提到为什么laravel这么好国内用的少,还有就是yii2,有人提到原因就是composer在国内无法使用.这制约了使用composer进行包管理的框架在国内的传播和 ...
随机推荐
- mysql级联删除
一个building对应多个rooms,building删除----级联删除相关的rooms 第一步, 创建buildings表,如下创建语句: USE testdb; CREATE TABLE bu ...
- Elasticsearch究竟要设置多少分片数?
0.引言 本文翻译自Elasticsearch20170918热乎的官方博客,原作者:Christian Dahlqvist. 在构建Elasticsearch集群的初期如果集群分片设置不合理,可能在 ...
- 使用seaborn制图(箱型图)
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # 设置风格, ...
- JSON解析工具比较,主要GSON和FastJSON
JSON解析工具比较,主要GSON和FastJSON 一 .各个JSON技术的简介和优劣 1.json-lib json-lib最开始的也是应用最广泛的json解析工具,json-lib 不好的地方确 ...
- Firefox内存占用过高解决方法
刚开始使用firefox火狐浏览器的时候,你会发现firefox占用内存大,CPU占用率高,打开网页停顿等问题,其实这些是因为firefox没有进行优化,默认设置是标准的设置的原因,解决方法如下: 一 ...
- 数据库的 ACID 属性
ACID(Atomicity 原子性.Consistency 一致性.Isolation 隔离性.Durability 持久性)是一系列属性. 这些属性保证了数据库事物的可靠.在数据库中,对数据的一系 ...
- 遍历Datatable
//方法一 DataSet dataSet = new DataSet(); DataTable dt = dataSet.Tables[]; ; i < dt.Rows.Count; i++) ...
- 在eclipse的web项目里面创建jsp时第一行报错
原因是因为项目里面没有配置tomcat,配置一下tomcat就好了
- Centos7搭建pptp一键安装脚本
废话不多说,先上脚本地址:Centos7一键pptp 使用: wget http://files.cnblogs.com/files/wangbin/CentOS7-pptp-host1plus.sh ...
- mongodb基础学习5-索引
下面来看看索引,有btree索引和hash索引,会提高查询速度,但降低了写入速度,可以按升,降序建立 包括单列索引,多列索引,子文档索引,也可分为普通索引,惟一索引,稀疏索引,hash索引(2.4新增 ...