`
~~~
<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/3/19-10:05
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
namespace Jrk;
class Http
{
/**
* @param $url
* @param array $params
* @param array $options
* @return mixed|string
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/19
* @name: post
* @describe:发送一个POST请求
*/
public static function post($url, $params = [], $options = [])
{
$req = self::sendRequest($url, $params, 'POST', $options);
return $req['ret'] ? $req['msg'] : '';
}
/**
* @param $url
* @param array $params
* @param array $options
* @return mixed|string
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/19
* @name: get
* @describe: 发送一个GET请求
*/
public static function get($url, $params = [], $options = [])
{
$req = self::sendRequest($url, $params, 'GET', $options);
return $req['ret'] ? $req['msg'] : '';
}
/**
* @param $url
* @param array $params
* @param string $method
* @param array $options
* @return array
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/19
* @name: sendRequest
* @describe: CURL发送Request请求,含POST和REQUEST
*/
public static function sendRequest($url, $params = [], $method = 'POST', $options = [])
{
$method = strtoupper($method);
$protocol = substr($url, 0, 5);
$query_string = is_array($params) ? http_build_query($params) : $params;
$ch = curl_init();
$defaults = [];
if ('GET' == $method) {
$geturl = $query_string ? $url . (stripos($url, "?") !== false ? "&" : "?") . $query_string : $url;
$defaults[CURLOPT_URL] = $geturl;
} else {
$defaults[CURLOPT_URL] = $url;
if ($method == 'POST') {
$defaults[CURLOPT_POST] = 1;
} else {
$defaults[CURLOPT_CUSTOMREQUEST] = $method;
}
$defaults[CURLOPT_POSTFIELDS] = $query_string;
}
$defaults[CURLOPT_HEADER] = false;
$defaults[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36";
$defaults[CURLOPT_FOLLOWLOCATION] = true;
$defaults[CURLOPT_RETURNTRANSFER] = true;
$defaults[CURLOPT_CONNECTTIMEOUT] = 3;
$defaults[CURLOPT_TIMEOUT] = 3;
// disable 100-continue
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
if ('https' == $protocol) {
$defaults[CURLOPT_SSL_VERIFYPEER] = false;
$defaults[CURLOPT_SSL_VERIFYHOST] = false;
}
curl_setopt_array($ch, (array)$options + $defaults);
$ret = curl_exec($ch);
$err = curl_error($ch);
if (false === $ret || !empty($err)) {
$errno = curl_errno($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return [
'ret' => false,
'errno' => $errno,
'msg' => $err,
'info' => $info,
];
}
curl_close($ch);
return [
'ret' => true,
'msg' => $ret,
];
}
/**
* @param $url
* @param array $params
* @param string $method
* @return bool
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/19
* @name: sendAsyncRequest
* @describe:异步发送一个请求
*/
public static function sendAsyncRequest($url, $params = [], $method = 'POST')
{
$method = strtoupper($method);
$method = $method == 'POST' ? 'POST' : 'GET';
//构造传递的参数
if (is_array($params)) {
$post_params = [];
foreach ($params as $k => &$v) {
if (is_array($v)) {
$v = implode(',', $v);
}
$post_params[] = $k . '=' . urlencode($v);
}
$post_string = implode('&', $post_params);
} else {
$post_string = $params;
}
$parts = parse_url($url);
//构造查询的参数
if ($method == 'GET' && $post_string) {
$parts['query'] = isset($parts['query']) ? $parts['query'] . '&' . $post_string : $post_string;
$post_string = '';
}
$parts['query'] = isset($parts['query']) && $parts['query'] ? '?' . $parts['query'] : '';
//发送socket请求,获得连接句柄
$fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 3);
if (!$fp) {
return false;
}
//设置超时时间
stream_set_timeout($fp, 3);
$out = "{$method} {$parts['path']}{$parts['query']} HTTP/1.1\r\n";
$out .= "Host: {$parts['host']}\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "Content-Length: " . strlen($post_string) . "\r\n";
$out .= "Connection: Close\r\n\r\n";
if ($post_string !== '') {
$out .= $post_string;
}
fwrite($fp, $out);
//echo fread($fp, 1024);
fclose($fp);
return true;
}
/**
* @param $file
* @param bool $delaftersend
* @param bool $exitaftersend
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/19
* @name: sendToBrowser
* @describe:发送文件到客户端
*/
public static function sendToBrowser($file, $delaftersend = true, $exitaftersend = true)
{
if (file_exists($file) && is_readable($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;filename = ' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check = 0, pre-check = 0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
if ($delaftersend) {
unlink($file);
}
if ($exitaftersend) {
exit;
}
}
}
}
~~~
`
- 空白目录
- thinkphp5
- tools-常用类库
- redis类库
- Excel类库
- File文件操作类库
- Http请求类库
- Maile邮件发送
- Hooks行为钩子
- 七牛云
- 随机数和字符串生成
- 字符串处理
- 时间类处理
- tree型转换
- 工具类库
- 文件打包下载
- 常用功能
- 文件上传
- php生成word文档
- elasticsearch 基本搜索
- 使用jwt开发API接口
- 安装模及搭建
- ApiCheck.php
- ApiCheckLogin.php
- common.php
- Login.php
- Comment.php
- 汉字转拼音
- 安装使用
- Pinyin类
- elasticsearch操作
- 常用方法
- 数据源生成layui-select
- 获取自定义配置项
- 百度编辑器
- 格式化文件大小
- 多语言设置
- hook监听
- 域名绑定到模块
- thinkphp6
- 文件上传
- tp5totp6
- 创建路径
- 获取类所有方法
- password_hash加密验证
- 生成 qrcode
- 邮件发送
- 获取QQ信息
- GoogleAuthenticator
- redis限流
- redis 加锁
- 百度翻译
- QueryList爬取数据
- 获取时间类
- 命令
- Git常用命令
- easyswoole
- pix_qrcode
- 验证 cpf,cnpj
- php常用方法
- 日志
- 卡通头像
- 两位小数
- 图片转base64
- auth加密解密
- phpoffice/phpspreadsheet导入导出
- fastadmin
- 树结构
- 单选框
- 复选框
- 二级搜索
- select选择框
- selectpage选中回调事件
- 标签添加
- 修改where条件
- 表格列表中添加input框
- selectpage事件
- fieldlist
- js操作
- test_js
- 多表格
- template模板