curl get & post 示例
CURL 个人觉得没必要背,所以我记录在此两个方法
GET请求:
function curl_get($url,$is_ssl=FALSE) {//模拟浏览器get请求 $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 5000); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, Is_SSL);//ssl 不验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, Is_SSL);//ssl 不验证 curl_setopt($ch, CURLOPT_URL,$url ); $res = curl_exec($ch); echo $res; }
POST请求:
function curl_post($url, $params=[], $header=[]) {//模拟浏览器post请求 $ch = curl_init(); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 5000); curl_setopt($ch, CURLOPT_POST, 1 ); if(is_array($params) && !empty($params) ) { $post_param = http_build_query($params);//构造url参数地址 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_param); } if(is_array($header) && !empty($header)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//请求的头信息数组 } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, Is_SSL);//ssl 不验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, Is_SSL);//ssl 不验证 curl_setopt($ch, CURLOPT_URL,$url ); $res = curl_exec($ch); echo $res; }
---- 始终相信这句:
----“做每天该做的事,不计结果!”
---- 因爲對於編程還只是新手,對很多知識掌握的不牢靠,歡迎大家批評指正~~|=-=|~~
----“做每天该做的事,不计结果!”
---- 因爲對於編程還只是新手,對很多知識掌握的不牢靠,歡迎大家批評指正~~|=-=|~~