get/post json请求
function getCurlData($url,$type="get", $headerData, $data=array(),$timeout = 30){
//对空格进行转义
$url = str_replace(' ','+',$url);
if ($type == "get") {
if (!empty($data) && is_array($data)) {
$arr = [];
foreach ($data as $k=>$v) {
$arr[] = $k."=".$v;
}
$str = implode("&",$arr);
if (strstr($url,"?")) {
$url .= "&".$str;
} else {
$url .= "?".$str;
}
}
}
$data = json_encode($data);
$headerRequest = [
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
];
$headerRequest = array_merge($headerRequest, $headerData);
$url = urldecode($url);
//echo $url ;exit;
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch,CURLOPT_TIMEOUT,$timeout); //定义超时3秒钟
if ($type == "post") {
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,
CURLOPT_HTTPHEADER,
$headerRequest
);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
//执行并获取url地址的内容
$output = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
list($responseHeader, $body) = explode("\r\n\r\n", $output, 2);
$headArr = explode("\r\n", $responseHeader);
$responseHeaderArr = [];
foreach ($headArr as $loop) {
$arr = explode(': ', $loop);
$responseHeaderArr[$arr[0]] = $arr[1];
}
$reponseBody = json_decode($body, true);
return [$responseHeaderArr, $reponseBody];
}
return ['', ''];
}
下载文件
function downCurl($url, $filePath,$type="get", $headerData=array(), $data=array(),$timeout = 30)
{
$accessToken = $this->getAccessToken();
if (!$accessToken) {
return false;
}
//对空格进行转义
$url = str_replace(' ','+',$url);
if ($type == "get") {
if (!empty($data) && is_array($data)) {
$arr = [];
foreach ($data as $k=>$v) {
$arr[] = $k."=".$v;
}
$str = implode("&",$arr);
if (strstr($url,"?")) {
$url .= "&".$str;
} else {
$url .= "?".$str;
}
}
}
$data = json_encode($data);
$headerRequest = [
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
];
$headerRequest = array_merge($headerRequest, $headerData);
$url = urldecode($url);
//初始化
$ch = curl_init();
curl_setopt($ch,
CURLOPT_HTTPHEADER,
$headerRequest
);
//设置抓取的url
curl_setopt($ch, CURLOPT_URL, $url);
//打开文件描述符
$fp = fopen ($filePath, 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);
//这个选项是意思是跳转,如果你访问的页面跳转到另一个页面,也会模拟访问。
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_TIMEOUT, $timeout);
if ($type == "post") {
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,
CURLOPT_HTTPHEADER,
$headerRequest
);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
//执行命令
curl_exec($ch);
//关闭URL请求
curl_close($ch);
//关闭文件描述符
fclose($fp);
return true;
}
解压unzip
function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true){
if ($dest_dir) {
$dest_dir .= '/';
}
if ($zip = zip_open($src_file)){
if ($zip){
$splitter = ($create_zip_name_dir === true) ? "." : "/";
if($dest_dir === false){
$dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
}
// 如果不存在 创建目标解压目录
create_dirs($dest_dir);
// 对每个文件进行解压
while ($zip_entry = zip_read($zip)){
// 文件不在根目录
$pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
if ($pos_last_slash !== false){
// 创建目录 在末尾带
create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
}
// 打开包
if (zip_entry_open($zip,$zip_entry,"r")){
// 文件名保存在磁盘上
$file_name = $dest_dir.zip_entry_name($zip_entry);
// 检查文件是否需要重写
if ($overwrite === true || $overwrite === false && !is_file($file_name)){
// 读取压缩文件的内容
$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
@file_put_contents($file_name, $fstream);
// 设置权限
chmod($file_name, 0777);
}
// 关闭入口
zip_entry_close($zip_entry);
}
}
// 关闭压缩包
zip_close($zip);
}
}else{
return false;
}
return true;
}
function create_dirs($path){
if (!is_dir($path)){
$directory_path = "";
$directories = explode("/",$path);
array_pop($directories);
foreach($directories as $directory){
$directory_path .= $directory."/";
if (!is_dir($directory_path)){
mkdir($directory_path);
chmod($directory_path, 0777);
}
}
}
}
压缩文件
function zip($srcFile,$descFile,$zipName){
if(!$srcFile){
return false;
}
if(!$descFile){
$descFile='/';
}
create_dirs($descFile);
$zip = new \ZipArchive();
if ($zip->open($descFile.$zipName.'.zip', \ZipArchive::CREATE) === TRUE)
{
// 将文件添加到zip文件中的demo_folder文件夹内
addFile($srcFile, 0,$zip);
$zip->addFromString('readme.txt',"欢迎光临");
// 关闭zip文件
$zip->close();
}
}
function addFile($srcDir,$level=0,$zip) {
//首先先读取文件夹
$temp=scandir($srcDir);
$level++;
//遍历文件夹
foreach($temp as $v){
$a=$srcDir.'/'.$v;
if(is_dir($a)){//如果是文件夹则执行
if($v=='.' || $v=='..'){//判断是否为系统隐藏的文件.和.. 如果是则跳过
continue;
}
addFile($a, $level,$zip);//因为是文件夹所以再次调用 selectdir,把这个文件夹下的文件遍历出来
}else{
$zip->addFile($a,$v);
}
}
}