public function _getData($url, $changeCode = true, $clearCode = true) { $getcontent = $this->getcontnet($url); $content = ''; if ($changeCode) { $getcontent = autoCharset($getcontent, 'gbk', 'utf-8'); } if ($this->strCut) { $getcontent = $this->strCut($getcontent, $this->strCut[0], $this->strCut[1]); } if ($clearCode) { $content = clearStrSpace($this->clear($getcontent)); } if ($content) { //避免因清理造成内容丢失 $getcontent = $content; unset($content); } $ostr = array('charset=GBK', 'charset=gb2312', '&'); $rstr = array('charset=utf-8', 'charset=utf-8', '&'); if ($this->replace) { foreach ($this->replace as $key => $val) { $ostr[] = $key; $rstr[] = $val; } } $getcontent = str_replace($ostr, $rstr, $getcontent); // echo $getcontent;exit; $this->_setDocument($getcontent); return $getcontent; }
private function generateXML($filename = 'excel-report', $in = 'UTF8', $out = 'gbk') { header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding); header("Content-Disposition: inline; filename=\"" . autoCharset($filename, $in, $out) . ".xls\""); echo stripslashes(sprintf($this->header, $this->sEncoding)), "\n<Worksheet ss:Name=\"" . $this->sWorksheetTitle . "\">\n<Table>\n", implode('', $this->lines), "</Table>\n</Worksheet>\n", $this->footer; }
/** * 私有方法:执行最终请求 * * @param string $method :HTTP请求方式 * @param string $url :请求的URL * @param array $params :请求的参数 * @param array $uploadFile :上传的文件(只有POST时才生效) * @param array $referer :引用页面 * @return 错误返回:false 正确返回:结果内容 */ private function _request($method, $url, $params = array(), $uploadFile = array(), $referer = '') { //如果是以GET方式请求则要连接到URL后面 if ($method == 'GET') { $url = $this->_parseUrl($url, $params); } //设置请求的URL curl_setopt($this->ch, CURLOPT_URL, $url); //如果是POST if ($method == 'POST') { //发送一个常规的POST请求,类型为:application/x-www-form-urlencoded curl_setopt($this->ch, CURLOPT_POST, true); //设置POST字段值 $postData = $this->_parsmEncode($params); //如果有上传文件 if ($uploadFile) { foreach ($uploadFile as $key => $file) { if (is_array($file)) { $n = 0; foreach ($file as $f) { //文件必需是绝对路径 $postData[$key . '[' . $n++ . ']'] = '@' . $f; } } else { $postData[$key] = '@' . $file; } } } //print_r($postData);die; curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postData); } if ($method == 'DELETE') { curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->_parsmEncode($params)); } if ($method == 'PUT') { curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->_parsmEncode($params)); } //设置了引用页,否则自动设置 if ($referer) { curl_setopt($this->ch, CURLOPT_REFERER, $referer); } else { curl_setopt($this->ch, CURLOPT_AUTOREFERER, true); } //得到所有设置的信息 $this->info['before'] = curl_getinfo($this->ch); //开始执行请求 $result = curl_exec($this->ch); //得到报文头 $headerSize = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE); $this->info['header'] = substr($result, 0, $headerSize); //去掉报文头 $result = substr($result, $headerSize); //得到所有包括服务器返回的信息 $this->info['after'] = curl_getinfo($this->ch); //如果请求成功 if ($this->errno() == 0) { //&& $this->info['after']['http_code'] == 200 if ($this->incode != 'utf-8') { $result = autoCharset($result, $this->incode, 'utf-8'); } return $result; } else { return false; } }
private function input_csv($handle, $in, $out) { $outData = array(); $n = 0; while ($data = fgetcsv($handle, 10000)) { $num = count($data); for ($i = 0; $i < $num; $i++) { $outData[$n][$i] = autoCharset($data[$i], $in, $out); } $n++; } return $outData; }
function autoCharset($fContents, $from = 'gbk', $to = 'utf-8') { $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from; $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to; if (strtoupper($from) === strtoupper($to) || empty($fContents) || is_scalar($fContents) && !is_string($fContents)) { return $fContents; } if (is_string($fContents)) { if (function_exists('mb_convert_encoding')) { return mb_convert_encoding($fContents, $to, $from); } elseif (function_exists('iconv')) { return iconv($from, $to, $fContents); } else { return $fContents; } } elseif (is_array($fContents)) { foreach ($fContents as $key => $val) { $_key = autoCharset($key, $from, $to); $fContents[$_key] = autoCharset($val, $from, $to); if ($key != $_key) { unset($fContents[$key]); } } return $fContents; } else { return $fContents; } }