1
0
Files
helper/application/common.php
2020-08-06 14:58:51 +08:00

87 lines
2.6 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
//
function get_array_url($k = '', $value = '', $emp = '')
{
$url = \think\Request::instance()->get();
unset($url['s']);
if ($k) {
if (isset($url[$k])) {
if ($value) {
$url[$k] = $value;
} else {
unset($url[$k]);
}
} else {
if ($value) {
$url[$k] = $value;
}
}
}
if ($emp) {
$array = explode(',', $emp);
if (is_array($array)) {
foreach ($array as $key => $value) {
if (isset($url[$value])) {
unset($url[$value]);
}
}
}
}
return http_build_query($url);
}
/**
* curl操作函数
* @param string $url 请求地址
* @param string $method 提交方式
* @param array $postFields 提交内容
* @param array $header 请求头
* @return mixed 返回数据
*/
function http($url, $method = "GET", $postFields = null, $headers = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
if (!is_null($postFields)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
//https request
if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
if (is_array($headers) && 0 < count($headers)) {
$httpHeaders = [];
foreach ($headers as $key => $value) {
array_push($httpHeaders, $key . ":" . $value);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
}
$data = curl_exec($ch);
$err = curl_errno($ch);
curl_close($ch);
if ($err > 0) {
return curl_error($ch);
} else {
return $data;
}
}