/** * 添加上传文件 * * @param $file_name string 文件路径 * @param $name string 文件名 * @return $this */ public function _addFile($name, $fileName = '', $mimeType = '') { if (class_exists('\\CURLFile')) { $this->files[$name] = new \CURLFile(realpath($fileName), MimeType::getMimeType($fileName), basename($fileName)); $this->setOption(CURLOPT_SAFE_UPLOAD, true); } else { $this->files[$name] = '@' . realpath($fileName); } return $this; }
/** * 创建一个CURL对象 * * @param string $url URL地址 * @param int $timeout 超时时间 * @return fsockopen() */ public function _create($url) { $matches = parse_url($url); $hostname = $matches['host']; $uri = isset($matches['path']) ? $matches['path'] . (isset($matches['query']) ? '?' . $matches['query'] : '') : '/'; $connPort = isset($matches['port']) ? intval($matches['port']) : ($matches['scheme'] == 'https' ? 443 : 80); if ($matches['scheme'] == 'https') { $connHost = $this->hostIp ? 'tls://' . $this->hostIp : 'tls://' . $hostname; } else { $connHost = $this->hostIp ? $this->hostIp : $hostname; } $header = ['Host' => $hostname, 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Encoding' => 'gzip, deflate', 'Connection' => 'Close']; if (!is_null($this->authorizationToken)) { // 认证 $header['Authorization'] = $this->authorizationToken; } if ($this->userAgent) { $header['User-Agent'] = $this->userAgent; } elseif (array_key_exists('HTTP_USER_AGENT', $_SERVER)) { $header['User-Agent'] = $_SERVER['HTTP_USER_AGENT']; } else { $header['User-Agent'] = "PHP/" . PHP_VERSION . " HttpClient/1.4.7"; } if ($this->referer) { $header['Referer'] = $this->referer; } if ($this->cookie) { $header['Cookie'] = is_array($this->cookie) ? http_build_query($this->cookie, '', ';') : $this->cookie; } if ($this->header) { foreach ($this->header as $item) { // 防止有重复的header if (preg_match('#(^[^:]*):(.*)$#', $item, $m)) { $header[trim($m[1])] = trim($m[2]); } } } if ($this->files) { $boundary = '----------------------------' . substr(md5(microtime(1) . mt_rand()), 0, 12); $vars = "--{$boundary}\r\n"; if ($this->postData[$url]) { if (!is_array($this->postData[$url])) { parse_str($this->postData[$url], $post); } else { $post = $this->postData[$url]; } // form data foreach ($post as $key => $val) { $vars .= "Content-Disposition: form-data; name=\"" . rawurlencode($key) . "\"\r\n"; $vars .= "Content-type:application/x-www-form-urlencoded\r\n\r\n"; $vars .= rawurlencode($val) . "\r\n"; $vars .= "--{$boundary}\r\n"; } } foreach ($this->files as $name => $filename) { $vars .= "Content-Disposition: form-data; name=\"" . $name . "\"; filename=\"" . rawurlencode(basename($filename)) . "\"\r\n"; $vars .= "Content-Type: " . MimeType::getMimeType($filename) . "\r\n\r\n"; $vars .= file_get_contents($filename) . "\r\n"; $vars .= "--{$boundary}\r\n"; } $vars .= "--\r\n\r\n"; $header['Content-Type'] = 'multipart/form-data; boundary=' . $boundary; } else { if (isset($this->postData[$url]) && $this->postData[$url]) { // 设置POST数据 $vars = is_array($this->postData[$url]) ? http_build_query($this->postData[$url]) : (string) $this->postData[$url]; $header['Content-Type'] = 'application/x-www-form-urlencoded'; } else { $vars = ''; } } // 设置长度 $header['Content-Length'] = strlen($vars); if (!is_null($this->proxyHost) && !is_null($this->proxyPort)) { $connHost = $this->proxyHost; $connPort = $this->proxyPort; $str = $this->method . ' ' . $url . ' HTTP/1.1' . "\r\n"; } else { $str = $this->method . ' ' . $uri . ' HTTP/1.1' . "\r\n"; } foreach ($header as $k => $v) { $str .= $k . ': ' . str_replace(["\r", "\n"], '', $v) . "\r\n"; } $str .= "\r\n"; if ($this->timeout > ini_get('max_execution_time')) { @set_time_limit($this->timeout); } $ch = @fsockopen($connHost, $connPort, $errno, $errstr, $this->timeout); if (!$ch) { // \Leaps\Debug::error ( "$errstr ($errno)" ); return false; } else { stream_set_blocking($ch, TRUE); fwrite($ch, $str); if ($vars) { // 追加POST数据 fwrite($ch, $vars); } return $ch; } }