QueryList 内置插件:如何利用 guzzleHTTP 发出 get 请求
发布于 作者:苏南大叔 来源:程序如此灵动~

本文说的是querylist
的一个内置插件:GuzzleHTTP
插件。看名字就应该猜测到,是一个get()
和post()
的包装类库。
苏南大叔理解着:就是一个curl
或者file_get_contents
的包装罢了。反正类似的包装也会有很多,以前苏南大叔也没少发明类似的轮子。anyway,下面的文字是个官方说明。本文不是用于详细探讨这个Guzzle
类库如何使用的,所以,大家如果感兴趣,可以自行点击链接,查看相关文档。
Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送请求,并集成到我们的WEB服务上。
QueryList get($url,$args = null,$otherArgs = [])
Http get插件,用来轻松获取网页。该插件基于GuzzleHttp
,请求参数与它一致。
$ql = QueryList::get('http://httpbin.org/get?param1=testvalue');
echo $ql->getHtml();
等价于下面操作:
$html = file_get_contents('http://httpbin.org/get?param1=testvalue');
$ql = QueryList::html($html);
echo $ql->getHtml();
带url请求参数
$ql->get('http://httpbin.org/get',[
'param1' => 'testvalue',
'params2' => 'somevalue'
]);
$ql->get('http://httpbin.org/get','param1=testvalue& params2=somevalue');
echo $ql->getHtml();
输出:
{
"args": {
"param1": "testvalue",
"params2": "somevalue"
},
"headers": {
"Connection": "close",
"Host": "httpbin.org",
"Referer": "http://httpbin.org/get",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
},
"origin": "112.97.*.*",
"url": "http://httpbin.org/get?param1=testvalue?ms2=somevalue"
}
携带Cookie采集需要登录的页面
//采集新浪微博需要登录才能访问的页面
$ql = QueryList::get('http://weibo.com',[],[
'headers' => [
//填写从浏览器获取到的cookie
'Cookie' => 'SINAGLOBAL=546064; wb_cmtLike_2112031=1; wvr=6;....'
]
]);
//echo $ql->getHtml();
echo $ql->find('title')->text();
//输出: 我的首页 微博-随时随地发现新鲜事
http插件默认已经开启了cookie功能,当然你也可以手动设置cookie。
$cookieJar = new \GuzzleHttp\Cookie\CookieJar();
$ql = QueryList::get('https://www.baidu.com/',[],[
'cookies' => $cookieJar
]);
伪造浏览器请求头部信息
$ql->get('http://httpbin.org/get',[
'param1' => 'testvalue',
'params2' => 'somevalue'
],[
'headers' => [
'Referer' => 'https://querylist.cc/',
'User-Agent' => 'testing/1.0',
'Accept' => 'application/json',
'X-Foo' => ['Bar', 'Baz'],
'Cookie' => 'abc=111;xxx=222'
]
]);
echo $ql->getHtml();
输出:
{
"args": {
"param1": "testvalue",
"params2": "somevalue"
},
"headers": {
"Accept": "application/json",
"Connection": "close",
"Cookie": "abc=111;xxx=222",
"Host": "httpbin.org",
"Referer": "https://querylist.cc/",
"User-Agent": "testing/1.0",
"X-Foo": "Baz"
},
"origin": "112.97.*.*",
"url": "http://httpbin.org/get?param1=testvalue?ms2=somevalue"
}
使用Http代理
$ql->get('http://httpbin.org/get',[
'param1' => 'testvalue',
'params2' => 'somevalue'
],[
'proxy' => 'http://222.141.11.17:8118',
//设置超时时间,单位:秒
'timeout' => 30,
'headers' => [
'Referer' => 'https://querylist.cc/',
'User-Agent' => 'testing/1.0',
'Accept' => 'application/json',
'X-Foo' => ['Bar', 'Baz'],
'Cookie' => 'abc=111;xxx=222'
]
]);
echo $ql->getHtml();
输出:
{
"args": {
"param1": "testvalue",
"params2": "somevalue"
},
"headers": {
"Accept": "application/json",
"Connection": "close",
"Cookie": "abc=111;xxx=222",
"Host": "httpbin.org",
"Proxy-Connection": "Keep-Alive",
"Referer": "https://querylist.cc/",
"User-Agent": "testing/1.0",
"X-Foo": "Baz"
},
"origin": "222.141.11.17",
"url": "http://httpbin.org/get?param1=testvalue?ms2=somevalue"
}
相关链接
GuzzleHTTP
是一款功能非常强大的Http客户端,你需要的Http功能它都有,更多用法可以查看GuzzleHTTP文档:
总结
GuzzleHTTP
是内置的client,所以,使用get()
方法吧!很简单的。


