http请求curl

/**************************************************************
 * Function name: http_request
 * Description: https请求(支持GET和POST)
 * Param: 参数说明
 * 【in】$url string 请求url
 * 【in】$data array 请求发送数据
 * 【in】$headers array 请求头发送数据
 * 【out】$output
 *******************************************************************/

function http_request($url,$data = null,$headers = []){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if(!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    if (!empty($headers)) {
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    }
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

 

posted @ 2019-08-05 22:22  好笑。  阅读(1209)  评论(0编辑  收藏  举报