GuzzleHttp设置代理
人寿保险没提供保单查询API,搜了一下没相关代码,手动撸了一个自动查保工具。
用GuzzleHttp发请求,header设置的有问题导致和预期不一致,所以抓包和浏览器的请求对比。
Charles监听的8888端口,GuzzleHttp配置代理只需要增加proxy参数即可,部分代码如下:
$client = new Client(['headers' => [
'Host' => $this->ip,
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:73.0) Gecko/20100101 Firefox/73.0',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language' => 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Origin' => 'http://'.$this->ip,
'Referer' => 'http://'.$this->ip.'/ant/QueryConditions.jsp',
'Upgrade-Insecure-Requests' => '1',
'Pragma' => 'no-cache',
'Cache-Control' => 'no-cache',
'Accept-Encoding' => 'gzip, deflate',
'Connection' => 'keep-alive',
]]);
$res = $client->request('GET', $url, [
'cookies' => $jar,
'proxy' => 'http://localhost:8888',
]);
相关文档:
https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html#proxy