file_get_contents傳送post請求

declandragon發表於2022-01-28
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 '';
        }
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結
感謝閱讀,有收穫的話不妨點個贊:smiling_imp:

相關文章