thinkphp5.1 企业微信群机器人发送文件 2023-0

2023-02-25  本文已影响0人  阿然学编程
class QywxServer
{
    /**
     * curl文件上传
     * @param $url  上传接口url
     * @param $file_path    上传文件路径
     * @return bool|string
     */
    function curl_file($url, $file_path)
    {
        $httpInfo = array();
        
        $fileName = basename($file_path);//获取带后缀的文件名
        $mimeType = mime_content_type($file_path);//获取文件mime类型

        // 创建一个 `curl` 实例
        $ch = curl_init();

        // 设置请求 URL
        curl_setopt($ch, CURLOPT_URL, $url);

        //创建一个 CURLFile 对象, 用与上传文件。
        $data = array(
            'media' => curl_file_create(realpath($file_path), $mimeType, $fileName)
        );
        // 设置请求方法为 POST
        curl_setopt($ch, CURLOPT_POST, true);
        // 设置请求正文为 multipart/form-data 格式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        //将获取的信息以文件流的形式返回,而不是直接输出在浏览器
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        //跳过https证书验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        //CURL_HTTP_VERSION_1_0 (强制使用 HTTP/1.0)或CURL_HTTP_VERSION_1_1 (强制使用 HTTP/1.1)
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

        // 执行请求
        $output = curl_exec($ch);

        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);//http状态码
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));//获取cURL连接资源句柄的信息。

        // 检查是否出现错误
        if (curl_errno($ch)) {
            return 'Error: ' . curl_error($ch);
        }
        // 关闭 `curl` 实例
        curl_close($ch);
        if (!$output) return false;

        return json_decode($output, true);//Json对象转换数组
    }

    /**
     * @param $url
     * @param $params
     * @param bool $https
     * @param int $timeOut
     * @return mixed
     */
    function jsonPostUrl($url, $params, $timeOut = 3)
    {
        $httpInfo = array();
        if (is_array($params)) {
            //数组转json解决中文乱码 + 不转义反斜杠
            //JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES = 320
            $params = json_encode($params, 320);
        }
        $ch = curl_init();//初始化一个cURL会话
        curl_setopt($ch, CURLOPT_URL, $url);//获取的URL地址
        curl_setopt($ch, CURLOPT_POST, true);//发送POST请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        //设置Http请求头需以数组方式进行设置:
        //array('Content-Type: application/json; charset=utf-8','Content-Length:' . strlen($params));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length:' . strlen($params)
        ));
        //设置超时限制防止死循环,发起连接前等待的时间,如果设置为0,则无限等待
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeOut);
        //设置cURL允许执行的最长秒数
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut * 10);
        //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //显示header头文件的信息 true显示,false不显示
        curl_setopt($ch, CURLOPT_HEADER, false);
        //跳过https证书验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        //CURL_HTTP_VERSION_1_0 (强制使用 HTTP/1.0)或CURL_HTTP_VERSION_1_1 (强制使用 HTTP/1.1)
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        $output = curl_exec($ch);//执行会话
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);//http状态码
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));//获取cURL连接资源句柄的信息。
        curl_close($ch);//关闭cURL资源,并且释放系统资源
        if (!$output) return false;
        return json_decode($output, true);//Json对象转换数组
    }

    public function send()
    {
        //一 上传文件获取media_id
        $file = '../data.csv';//文件路径
        //企业微信群机器人上传url
        $uploadUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=替换自己的群机器人key&type=file";
        //上传文件
        $upRes = $this->curl_file($uploadUrl, $file);
        //获取media_id
        $media_id = $upRes['media_id'];
        dump($media_id);
        //二 发送文件到企业微信群
        $data = array(
            'msgtype' => 'file',
            'file' => array(
                'media_id' => $media_id
            )
        );
        $url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=替换自己的群机器人key";

        $sendRes = $this->jsonPostUrl($url, $data);
        dump($sendRes);
        die;

    }
    
}

效果图如下:


image.png
上一篇 下一篇

猜你喜欢

热点阅读