ManaPHP\Http\Client::_request PHP Method

_request() public method

public _request ( string $type, string $url, string | array $data, array $headers, array $options ) : integer
$type string
$url string
$data string | array
$headers array
$options array
return integer
    public function _request($type, $url, $data, $headers, $options)
    {
        $this->_curlResponseHeader = [];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);
        if ($options['max_redirects'] > 0) {
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($curl, CURLOPT_MAXREDIRS, $options['max_redirects']);
        } else {
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
        }
        if (isset($headers['Cookie'])) {
            curl_setopt($curl, CURLOPT_COOKIE, $headers['Cookie']);
        }
        if (is_array($data)) {
            $hasFiles = false;
            /** @noinspection ForeachSourceInspection */
            foreach ($data as $k => $v) {
                if (is_string($v) && $v[0] === '@') {
                    $hasFiles = true;
                    if (class_exists('CURLFile')) {
                        $file = substr($v, 1);
                        $parts = explode(';', $file);
                        if (count($parts) === 1) {
                            /** @noinspection AlterInForeachInspection */
                            $data[$k] = new \CURLFile($file);
                        } else {
                            $file = $parts[0];
                            $types = explode('=', $parts[1]);
                            if ($types[0] !== 'type' || count($types) !== 2) {
                                throw new ClientException('`:file` file name format is invalid', ['file' => $v]);
                            } else {
                                /** @noinspection AlterInForeachInspection */
                                $data[$k] = new \CURLFile($file, $types[1]);
                            }
                        }
                    }
                } elseif (is_object($v)) {
                    $hasFiles = true;
                }
            }
            if (!$hasFiles) {
                $data = http_build_query($data);
            }
        }
        switch ($type) {
            case 'GET':
                break;
            case 'POST':
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                break;
            case 'PATCH':
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                break;
            case 'PUT':
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                break;
            case 'DELETE':
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
                break;
        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_TIMEOUT, $options['timeout']);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $options['timeout']);
        curl_setopt($curl, CURLOPT_REFERER, isset($headers['Referer']) ? $headers['Referer'] : $url);
        curl_setopt($curl, CURLOPT_USERAGENT, $headers['User-Agent']);
        unset($headers['Referer'], $headers['User-Agent'], $headers['Cookie']);
        $formatted_headers = [];
        foreach ($headers as $k => $v) {
            if (is_int($k)) {
                $formatted_headers[] = $v;
            } else {
                $formatted_headers[] = $k . ': ' . $v;
            }
        }
        curl_setopt($curl, CURLOPT_HTTPHEADER, $formatted_headers);
        if ($options['proxy']) {
            curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
            curl_setopt($curl, CURLOPT_PROXY, $options['proxy']);
        }
        if ($options['ssl_certificates']) {
            if ($this->_peek && $this->_options['proxy'] !== '') {
                curl_setopt($curl, CURLOPT_CAINFO, $this->alias->resolve('@manaphp/Http/Client/fiddler.cer'));
            } else {
                curl_setopt($curl, CURLOPT_CAINFO, $this->alias->resolve($options['ssl_certificates']));
            }
        } else {
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        }
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $options['verify_host'] ? 2 : 0);
        $this->_responseBody = curl_exec($curl);
        /** @noinspection NotOptimalIfConditionsInspection */
        if (curl_errno($curl) === 23 || curl_errno($curl) === 61) {
            curl_setopt($curl, CURLOPT_ENCODING, 'none');
            $this->_responseBody = curl_exec($curl);
        }
        if (curl_errno($curl)) {
            throw new ClientException('cURL error: :code::message', ['code' => curl_errno($curl), 'message' => curl_error($curl)]);
        }
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $this->_curlResponseHeader = curl_getinfo($curl);
        curl_close($curl);
        return $httpCode;
    }