get_info = true; $type = 'get'; } $this->init(); $this->param($param); $this->setRequest($type); if ($header) { $this->setHeader($header); } if (!$agent) { $agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'; } if ($agent) { $this->setAgent($agent); } if ($proxy) { $this->setProxy($proxy); } if ($refer) { $this->setRefer($refer); } if ($json) { $this->setJson($param); } elseif ($type == 'file') { $this->setFormData($param); } elseif ($type == 'post' || $type == 'put') { $this->setForm($param); } elseif ($param) { if (strpos($url, '?')) { $url .= '&'; } else { $url .= '?'; } $url .= http_build_query($param); } if (strpos($url, '??')) { $url = str_replace('??', '?', $url); } $this->setUrl($url); return $this; } public function log($log) { $this->log = $log; return $this; } private function init() { if (!$this->handle) { $this->handle = curl_init(); } } private function param(&$param) { if ($param && is_array($param) && isset($param[0])) { $temp = $param; $param = array(); foreach ($temp as $k => $v) { if (is_array($v)) { $param = array_merge($param, $v); } else { $param[$k] = $v; } } } } public function setStream($callback) { curl_setopt($this->handle, CURLOPT_BUFFERSIZE, 16384); // 检查是否断联,每10秒发送一次心跳 curl_setopt($this->handle,CURLOPT_TCP_KEEPALIVE,1); // 开启 curl_setopt($this->handle,CURLOPT_TCP_KEEPIDLE,10); // 空闲10秒问一次 curl_setopt($this->handle,CURLOPT_TCP_KEEPINTVL,10); // 每10秒问一次 curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, function ($ch, $data) use ($callback) { call_user_func($callback, $data); return strlen($data); }); return $this; } public function setRequest($type) { if ($type == 'post' || $type == 'file') { curl_setopt($this->handle, CURLOPT_POST, true); } elseif ($type == 'put' || $type == 'delete') { curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, strtoupper($type)); } else { curl_setopt($this->handle, CURLOPT_HTTPGET, true); } } public function setHeader($header) { if (is_array($header)) { foreach ($header as $k => $v) { if (is_string($k)) { $this->header[] = $k . ':' . $v; } else { $this->header[] = $v; } } } else { $this->header = explode("\n", $header); } } public function setAgent($agent) { curl_setopt($this->handle, CURLOPT_USERAGENT, $agent); } public function setProxy($proxy) { curl_setopt($this->handle, CURLOPT_PROXY, $proxy[0]); //代理服务器地址 curl_setopt($this->handle, CURLOPT_PROXYPORT, $proxy[1]); //代理服务器端口 curl_setopt($this->handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); } public function setRefer($refer) { curl_setopt($this->handle, CURLOPT_REFERER, $refer); } public function setJson($param) { if (is_array($param)) { if (isset($param['item_desc'])) { $param = json_encode($param, JSON_UNESCAPED_UNICODE); } else { $param = json_encode($param); } } $header['Content-Type'] = 'application/json'; $header['Content-Length'] = strlen($param); $this->setHeader($header); $this->setParam($param); } public function setFormData($param) { $header['Content-Type'] = 'multipart/form-data'; if (!is_array($param)) { $param = Dever::json_decode($param); } $this->setHeader($header); $this->setParam($param); } public function setForm($param) { $header['Content-Type'] = 'application/x-www-form-urlencoded'; if (!is_array($param)) { $array = Dever::json_decode($param); if (json_last_error() === JSON_ERROR_NONE) { $param = $array; } } if (is_array($param)) { $param = http_build_query($param); } $this->setHeader($header); $this->setParam($param); } public function setUrl($url) { $this->url = $url; curl_setopt($this->handle, CURLOPT_URL, $url); curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, true); } public function result($setting = false) { try { if ($setting) { $this->setting($setting); } if ($this->header) { curl_setopt($this->handle, CURLOPT_HTTPHEADER, $this->header); } curl_setopt($this->handle, CURLOPT_HEADER, false); if ($this->result_header) { $this->result_header = array(); curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, array($this, 'headerHandler')); } if (Dever::shell('debug')) { curl_setopt($this->handle, CURLINFO_HEADER_OUT, true); } curl_setopt($this->handle, CURLOPT_ACCEPT_ENCODING, 'gzip,deflate'); $result = curl_exec($this->handle); $debug = array(); if (Dever::shell('debug')) { $debug['request'] = curl_getinfo($this->handle, CURLINFO_HEADER_OUT); } elseif ($this->get_info) { $result = curl_getinfo($this->handle); } curl_close($this->handle); $data = $result; if (!Dever::shell('all') && is_array($data)) { $data = count($data) . ' records'; } $debug['url'] = $this->url; $debug['param'] = $this->param; $debug['result'] = $data; if ($this->log) { Dever::log($debug, 'curl'); } Dever::debug($debug, 'curl'); if (isset($setting['stream'])) { exit; } return $result; } catch (\Exception $e) { curl_close($this->handle); return 'error'; } } public function setting($setting = array()) { if ($setting) { $this->init(); foreach ($setting as $k => $v) { $method = 'set' . ucfirst($k); $this->$method($v); } } } public function setVerbose($verbose) { curl_setopt($this->handle, CURLOPT_VERBOSE, $verbose); } public function setUserPWD($userpwd) { curl_setopt($this->handle, CURLOPT_USERPWD, $userpwd); } public function setTimeOut($time) { curl_setopt($this->handle, CURLOPT_TIMEOUT, $time); } public function setCookie($cookie) { curl_setopt($this->handle, CURLOPT_COOKIE, $cookie); } public function setParam($param) { $this->param = $param; if (is_array($param)) { $param = http_build_query($param); } curl_setopt($this->handle, CURLOPT_POSTFIELDS, $param); } public function setEncode($encode) { curl_setopt($this->handle, CURLOPT_ENCODING, $encode); } public function setIp($ip) { $config['CLIENT-IP'] = $ip; $config['X-FORWARDED-FOR'] = $ip; $this->setHeader($config); } public function setResultHeader($value) { $this->result_header = $value; } public function header() { return $this->result_header; } private function headerHandler($curl, $headerLine) { $len = strlen($headerLine); $split = explode(':', $headerLine, 2); if (count($split) > 1) { $key = trim($split[0]); $value = trim($split[1]); $this->result_header[$key] = $value; } return $len; } public function resultCookie() { $result = $this->result(); list($header, $body) = explode("\r\n\r\n", $result, 2); preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches); $info['cookie'] = substr($matches[1][0], 1); $info['content'] = $body; return $info; } }