<?php
|
|
namespace app\common\toole;
|
|
/**
|
* DHKHTTP请求封装
|
* author 锦鲤来了(1197185312@qq.com)
|
*/
|
|
use GuzzleHttp\Client;
|
use GuzzleHttp\Psr7\MultipartStream;
|
use GuzzleHttp\Psr7\Request;
|
|
class Dhkhttp
|
{
|
private $header;
|
private $client;
|
private $dhkoptions;
|
|
/**
|
* 初始化
|
* Upload constructor.
|
* @param $cfg
|
*/
|
public function __construct($agent = 1, $test = 1)
|
{
|
$this->header = [];
|
$this->dhkoptions = [];
|
if ($agent == 1) {
|
$this->header['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Safari/537.36';
|
}
|
$this->client = new Client([
|
'timeout' => 60,
|
'http_errors' => false,
|
'verify' => false,
|
]);
|
}
|
|
public function dhksend($url, $method = 'POST', $dhkoptions = [])
|
{
|
$dhkoptions = array_merge($this->dhkoptions, $dhkoptions);
|
$log_filename = "dhkhttp_" . date('Ymd');
|
$log = root_path() . "/runtime/log/" . $log_filename . ".log";
|
try {
|
$res = $this->client->request($method, $url, $dhkoptions);
|
} catch (\Exception $e) {
|
$msg = $e->getMessage();
|
echo_dhklog($msg, '请求报错:', $log_filename, 'error');
|
return ['http_code' => 0, 'code' => 0, 'msg' => $e->getMessage(), 'data' => []];
|
}
|
$http_code = $res->getStatusCode();
|
$http_data = json_decode($res->getBody(), true);
|
$http_data['http_code'] = $http_code;
|
echo_dhklog($http_data, '请求结果:', $log_filename);
|
if ($method != 'GET') {
|
//file_put_contents($log, date('Ymd H:i:s') . "\r\n请求结果" . $http_code . ":\r\n" . $res->getBody() . "\r\n", FILE_APPEND);
|
}
|
if ($http_code == 200 or $http_code == 304) {
|
// 获取响应头
|
$headers = $res->getHeaders();
|
// 获取响应中的所有cookie
|
$cookies = $headers['Set-Cookie'] ?? [];
|
$headers['Cookie'] = '';
|
if ($cookies) {
|
// 格式化cookie为请求头可接受的格式
|
$cookieJar = null;
|
foreach ($cookies as $cookie) {
|
$cookieParts = parse_url($cookie);
|
parse_str($cookieParts['path'], $cookieParams);
|
$cookieName = key($cookieParams);
|
$cookieValue = $cookieParams[$cookieName];
|
$cookieJar[] = "$cookieName=$cookieValue";
|
}
|
$headers['Cookie'] = implode('; ', $cookieJar);
|
$http_data['Cookie'] = $headers['Cookie'];
|
}
|
return $http_data;
|
}
|
$msg = $http_data['msg'] ?? '';
|
if (!$msg) {
|
$http_data['code'] = 0;
|
$http_data['msg'] = '请求失败' . $http_code;
|
}
|
return $http_data;
|
}
|
public function dhkpost($url, $postdata = [], $header = [])
|
{
|
if (!isset($header['Content-Type']) or !$header['Content-Type']) {
|
$header['Content-Type'] = 'application/x-www-form-urlencoded';
|
}
|
$contype = $header['Content-Type'];
|
$header = array_merge($this->header, $header);
|
if (strpos($contype, 'json') !== false) {
|
$body = json_encode($postdata);
|
$dhkoptions = [
|
'headers' => $header,
|
'body' => $body,
|
];
|
} else {
|
$body = http_build_query($postdata);
|
$dhkoptions = [
|
'headers' => $header,
|
'form_params' => $postdata,
|
];
|
}
|
echo_dhklog($dhkoptions, '请求参数:', 'dhkhttp_' . date('Ymd'));
|
return $this->dhksend($url, 'POST', $dhkoptions);
|
}
|
public function dhkget($url, $header = [])
|
{
|
$dhkoptions = [];
|
$header = array_merge($this->header, $header);
|
if ($header) {
|
$dhkoptions['headers'] = $header;
|
}
|
return $this->dhksend($url, 'GET', $dhkoptions);
|
}
|
|
public function dhkupfile($url, $postdata = [])
|
{
|
$header = [
|
'Content-Type' => 'multipart/form-data',
|
];
|
$body = new \GuzzleHttp\Psr7\MultipartStream($postdata);
|
$dhkoptions = [
|
'headers' => $header,
|
'body' => $body,
|
];
|
return $this->dhksend($url, 'POST', $dhkoptions);
|
}
|
}
|