file_get_contents发送post请求

if (!function_exists('send_dd_msg')) {
    /**
     * 发送钉钉消息.
     *
     * @see https://developers.dingtalk.com/document/robots/custom-robot-access/title-r82-8g5-0sk
     *
     * @param $api
     * @param $content
     * @param $at
     *
     * @return string
     */
    function send_dd_msg($api, $content, $at = [])
    {
        try {
            $data = [
                'msgtype' => 'text',
                'text' => ['content' => $content],
            ];
            if ($at) {
                $data['at'] = $at;
            }
            $opts = [
                'http' => [
                    'method' => 'POST',
                    'header' => 'Content-Type: application/json;charset=utf-8',
                    'content' => json_encode($data),
                    'timeout' => 15,
                ],
            ];

            return file_get_contents($api, false, stream_context_create($opts));
        } catch (\Throwable $e) {
            return '';
        }
    }
}
php
本作品采用《CC 协议》,转载必须注明作者和本文链接
感谢阅读,有收获的话不妨点个赞:smiling_imp:
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!