123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php namespace Dever\Helper;
- use Dever;
- class Curl
- {
- private $handle;
- private $url;
- private $get_info = false;
- private $log = false;
- private $result_header = false;
- private $param = array();
- private $header = array();
- public function load($url, $param = false, $type = '', $json = false, $header = false, $agent = false, $proxy = false, $refer = false)
- {
- if ($type == 'get_info') {
- $this->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;
- }
- }
|