chengkun
2025-09-04 0ea78440cb53b7ecf0ef715e3f51d88918c5821d
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?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);
    }
}