码迷,mamicode.com
首页 > Web开发 > 详细

PHP curl 封装 GET及POST方法很不错的

时间:2017-06-21 15:59:51      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:info   https   param   运行   get   lin   head   http   verify   

public function curl_get($url, array $params = array(),$timeout=5){ $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); return $file_contents; } public function curl_post($url,array $params = array(),$timeout){ $ch = curl_init();//初始化curl curl_setopt($ch,CURLOPT_URL,$url);//抓取指定网页 curl_setopt($ch, CURLOPT_HEADER, 0);//设置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $params); $data = curl_exec($ch);//运行curl curl_close($ch); return($data);//输出结果 } public function curl_get_https($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11‘); $res = curl_exec($ch); $rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch) ; return $res; }

PHP curl 封装 GET及POST方法很不错的

标签:info   https   param   运行   get   lin   head   http   verify   

原文地址:http://www.cnblogs.com/php-linux/p/7059770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!