php公用方法

1.curl请求

function curlRequest(string $url, string $method, $data, array $header = ["Content-Type:application/json"],int $timeout = 10,$isCheckSSl = true)
    {
        $ch = curl_init();
        //设置请求地址
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!empty($timeout)){
            // 设置连接超时时间,单位是秒
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            // 设置请求超时时间,单位是秒
            curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        }
        //设置请求方法
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        // 请求参数
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        //设置请求头
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        //跳过ssl证书验证
        if ($isCheckSSl){
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        //设置curl_exec()的返回值以字符串返回
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }

你可能感兴趣的:(php,php,开发语言)