在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
https://mp.weixin.qq.com/s/8luqMEd8xt8oJxFLLCU1XA 文章正文 cURL是利用url语法规定传输文件和数据的工具。php中有curl拓展,一般用来实现网络抓取,模拟发送get post请求,文件上传。 在php中建立curl的基本步骤如下: 1 初始化 2 设置选项,包括url 3 执行并获取结果 4 释放curl句柄。 在工作和学习中,我也是时常用的curl。由于在使用curl设置选项时,各种选项比较难以记忆,需要参考,故在此记录下常用的一些例子,以便后来参考。 实例一 : 抓取网页数据(以拉手网开放api为例,也是get请求) <?php header("Content-type: text/html; charset=utf-8"); $ch = curl_init();//初始化 /*============开始设置curl各种选项================*/ curl_setopt($ch, CURLOPT_URL, "http://open.lashou.com/opendeals/lashou/city.xml"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $html = curl_exec($ch);//执行句柄,获取返回内容 curl_close($ch);//释放句柄 echo $html ?> 如果用这种方法发get请求,参数附加到url后面即可 如curl_setopt($ch, CURLOPT_URL, "http://localhost/tqj/date/p822.php?name=yyyyy"); 实例 2 利用curl发送post请求 <?php $uri = "http://localhost/tqj/date/p822.php"; // post参数数组 $data = array ( 'name' => 'tianquanjun', 'password' => 'tianquanjun', ); //初始化 $ch = curl_init (); //各种项设置,网上参考而来,可以查看php手册,自己设置 curl_setopt ( $ch, CURLOPT_URL, $uri ); curl_setopt ( $ch, CURLOPT_POST, 1 );//post方式 curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); //执行 $return = curl_exec ( $ch ); //释放 curl_close ( $ch ); print_r($return); ?> 实例三 :curl 过程调试与错误信息处理 <?php $uri = "http://localhost/tqj/date/p822.php"; // post参数数组 $data = array ( 'name' => 'tianquanjun', 'password' => 'tianquanjun', ); //初始化 $ch = curl_init (); //各种项设置,网上参考而来,可以查看php手册,自己设置 curl_setopt ( $ch, CURLOPT_URL, $uri ); curl_setopt ( $ch, CURLOPT_POST, 1 );//post方式 curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); //执行 $return = curl_exec ( $ch ); //容错机制 if($return === false){ var_dump(curl_error($ch)); } //curl_getinfo()获取各种运行中信息,便于调试 $info = curl_getinfo($ch); echo "执行时间".$info['total_time'].PHP_EOL; //释放 curl_close ( $ch ); print_r($return); ?>
其中利用curl_error()获取错误信息,curl_getinfo()获取运行相关信息。 实例四 上传图片,获取返回信息。 跨域上传图片,同时获取返回信息,这个就能大显身手。和post比较像,注意文件之前加一个@符号 <?php $uri = "http://localhost/tqj/date/p822.php"; // post参数数组 $data = array ( 'author' => 'tianquanjun', 'upload' => '@C:\Users\tianquanjun.DANGDANG\Pictures\a.jpg', ); //初始化 $ch = curl_init (); //各种项设置,网上参考而来,可以查看php手册,自己设置 curl_setopt ( $ch, CURLOPT_URL, $uri ); curl_setopt ( $ch, CURLOPT_POST, 1 );//post方式 curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); //执行 $return = curl_exec ( $ch ); //容错机制 if($return === false){ var_dump(curl_error($ch)); } //curl_getinfo()获取各种运行中信息,便于调试 $info = curl_getinfo($ch); echo "执行时间".$info['total_time'].PHP_EOL; //释放 curl_close ( $ch ); print_r($return); ?> 实例五 : curl批处理。 curl有一个高级特性,批处理句柄。允许打开多个curl链接。 批处理就是打开多个curl句柄,并把这些句柄指派给一个批处理句柄,然后在while循环里等待处理完毕。curl_multi_exec()算是称得上多线程处理,不过它还是属于异步的范畴。 <?php header("Content-type: text/html; charset=gbk"); $urls=array('http://www.baidu.com','http://www.qq.com/'); $ch=array(); //批处理句柄 $mh=curl_multi_init(); //打开多个curl句柄,并指派给一个批处理句柄 $ch[0]=curl_init($urls[0]); $ch[1]=curl_init($urls[1]); for($i=0;$i<2;$i++) { curl_setopt($ch[$i],CURLOPT_RETURNTRANSFER,1); curl_multi_add_handle($mh,$ch[$i]); } $running = NULL; do{ usleep(10000); curl_multi_exec($mh,$running);//实现批处理,可以看做curl多线程,实际是异步范畴 }while($running>0); $res=array(); for($j=0;$j<2;$j++) { $res[$j]=curl_multi_getcontent($ch[$j]); } //关闭句柄 for($k=0;$k<2;$k++) { curl_multi_remove_handle($mh,$ch[$k]); } curl_multi_close($mh); print_r($res); ?>
基本算是列举了常用的一些实例。要想灵活运用curl,还是得熟悉curl的各个设置项,这些设置项才是curl的灵魂。 ==========================亲测效果=============================================== getwether.php <?php $data = "theCityName=邯郸"; $uri = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"; //初始化 $obj = curl_init(); //设置 curl_setopt($obj, CURLOPT_URL, $uri); curl_setopt($obj, CURLOPT_HEADER, 0); //将头文件的信息作为数据流输出 curl_setopt($obj, CURLOPT_RETURNTRANSFER, 1); //执行后直接返回 curl_setopt($obj, CURLOPT_POST, 1); //post 方式 curl_setopt($obj, CURLOPT_POSTFIELDS, $data); //发送post请求参数 curl_setopt($obj,CURLOPT_USERAGENT, " user-agent:Mozilla/5.0 (Windows NT 6.1; rv:62.0) Gecko/20100101 Firefox/62.0");//浏览器头信息 curl_setopt($obj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded;charset=utf-8", "Content-Length:" . strlen($data))); //发送header头信息 $opt = curl_exec($obj); if ($opt === false) { var_dump(curl_error($obj)); } else { var_dump(curl_errno($obj));//int(0) $info = curl_getinfo($obj); echo '执行时间:'.$info['total_time'] . PHP_EOL; // echo $opt; print_r($opt); } curl_close($obj); var_dump(curl_errno($obj));//int(0) 返回最后一次的错误号
在cmd命令行中输入: php -f getwether.php >wether.txt int(0) 执行时间:0.187 <?xml version="1.0" encoding="utf-8"?> <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/"> <string>河北</string> <string>邯郸</string> <string>53892</string> <string>53892.jpg</string> <string>2018/10/17 22:45:23</string> <string>8℃/19℃</string> <string>10月17日 多云</string> <string>北风小于3级</string> <string>1.gif</string> <string>1.gif</string> <string>今日天气实况:气温:12℃;风向/风力:北风 1级;湿度:77%;紫外线强度:弱。空气质量:中。</string> <string>紫外线指数:弱,辐射较弱,涂擦SPF12-15、PA+护肤品。 健臻·血糖指数:易波动,血糖易波动,注意监测。 穿衣指数:较舒适,建议穿薄外套或牛仔裤等服装。 洗车指数:较适宜,无雨且风力较小,易保持清洁度。 空气污染指数:中,易感人群应适当减少室外活动。 </string> <string>8℃/18℃</string> <string>10月18日 多云</string> <string>南风小于3级</string> <string>1.gif</string> <string>1.gif</string> <string>9℃/19℃</string> <string>10月19日 多云</string> <string>南风小于3级</string> <string>1.gif</string> <string>1.gif</string> <string>邯郸市是河北省最古老的城市,也是我国古代著名的城邑。早在2000多年前的春秋初期,就有邯郸的建制,当时邯郸已是一个人口聚居的城市。战国时作为赵国都城历经158年,秦统一后分别设邯郸县、邯郸郡。当时的邯郸经济昌盛、繁荣发达,与长安、洛阳、开封、成都同称五大都城。邯郸位于河北省南部,地处太行山东麓、淦阳河上,是晋冀鲁豫四省接壤的腹地。邯郸市位于太行山东麓,属暖温带半湿润半干旱大陆季风性气候。特点是四季分明,雨量适中,秋、春两季短,冬、夏两季长。邯郸市的自然环境得天独厚,西倚太行山,东临滏阳河,地势西北高、东南低,中部丘陵起伏,盆地交错;并有沁河、渚河、输元河流经市区(皆属滏阳河支流),丰富的水利资源为农业生产提供了优良的条件。景观:黄梁梦吕仙祠、永年君臣上寿石刻、学步桥、磁州窑、古邺城、兰陵王墓、天子冢、响堂山石窟、磁山古文化遗址等。</string> </ArrayOfString> ====================爬去豆瓣电影api================================== <?php $uri="https://api.douban.com/v2/movie/in_theaters"; $data="city=邯郸&start=0&count=3"; $obj=curl_init(); curl_setopt($obj,CURLOPT_URL,$uri); curl_setopt($obj,CURLOPT_RETURNTRANSFER,1); curl_setopt($obj,CURLOPT_POSTFIELDS,$data); curl_setopt($obj,CURLOPT_POST,1); curl_setopt($obj,CURLOPT_SSL_VERIFYPEER,0); $opt=curl_exec($obj); if($opt===false){ var_dump(curl_error($obj)); }else{ $info=curl_getinfo($obj); /* echo '<pre>'; print_r($info); echo '</pre>';*/ $movies=json_decode($opt,true); echo count($movies); echo '<pre>'; print_r($movies); echo '</pre>'; } curl_close($obj); 结果: 5 Array ( [count] => 3 [start] => 0 [total] => 21 [subjects] => Array ( [0] => Array ( [rating] => Array ( [max] => 10 [average] => 8.1 [stars] => 40 [min] => 0 ) [genres] => Array ( [0] => 剧情 [1] => 动作 [2] => 犯罪 ) [title] => 无双 [casts] => Array ( [0] => Array ( [alt] => https://movie.douban.com/celebrity/1044899/ [avatars] => Array ( [small] => https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p35678.jpg [large] => https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p35678.jpg [medium] => https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p35678.jpg ) [name] => 周润发 [id] => 1044899 ) [1] => Array ( [alt] => https://movie.douban.com/celebrity/1041390/ [avatars] => Array ( [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p49475.jpg [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p49475.jpg [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p49475.jpg ) [name] => 郭富城 [id] => 1041390 ) [2] => Array ( [alt] => https://movie.douban.com/celebrity/1016668/ [avatars] => Array ( [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p146.jpg [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p146.jpg [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p146.jpg ) [name] => 张静初 [id] => 1016668 ) ) [collect_count] => 325710 [original_title] => 無雙 [subtype] => movie [directors] => Array ( [0] => Array ( [alt] => https://movie.douban.com/celebrity/1014716/ [avatars] => Array ( [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p3555.jpg [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p3555.jpg [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p3555.jpg ) [name] => 庄文强 [id] => 1014716 ) ) [year] => 2018 [images] => Array ( [small] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535096871.jpg [large] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535096871.jpg [medium] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535096871.jpg ) [alt] => https://movie.douban.com/subject/26425063/ [id] => 26425063 ) [1] => Array ( [rating] => Array ( [max] => 10 [average] => 7.4 [stars] => 40 [min] => 0 ) [genres] => Array ( [0] => 剧情 [1] => 动作 [2] => 武侠 ) [title] => 影 [casts] => Array ( [0] => Array ( [alt] => https://movie.douban.com/celebrity/1274235/ [avatars] => Array ( [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p805.jpg [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p805.jpg [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p805.jpg ) [name] => 邓超 [id] => 1274235 ) [1] => Array ( [alt] => https://movie.douban.com/celebrity/1004856/ [avatars] => Array ( [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1415690807.36.jpg [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1415690807.36.jpg [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1415690807.36.jpg ) [name] => 孙俪 [id] => 1004856 ) [2] => Array ( [alt] => https://movie.douban.com/celebrity/1275564/ [avatars] => Array ( [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1366015827.84.jpg [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1366015827.84.jpg [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1366015827.84.jpg ) [name] => 郑恺 [id] => 1275564 ) ) [collect_count] => 255137 [original_title] => 影 [subtype] => movie [directors] => Array ( [0] => Array ( [alt] => https://movie.douban.com/celebrity/1054398/ [avatars] => Array ( [small] => https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p568.jpg [large] => https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p568.jpg [medium] => https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p568.jpg ) [name] => 张艺谋 [id] => 1054398 ) ) [year] => 2018 [images] => Array ( [small] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2530513100.jpg [large] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2530513100.jpg [medium] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2530513100.jpg ) [alt] => https://movie.douban.com/subject/4864908/ [id] => 4864908 ) [2] => Array ( [rating] => Array ( [max] => 10 [average] => 5.1 [stars] => 25 [min] => 0 ) [genres] => Array ( [0] => 喜剧 ) [title] => 李茶的姑妈 [casts] => Array ( [0] => Array ( [alt] => https://movie.douban.com/celebrity/1363857/ [avatars] => Array ( [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1519453932.46.jpg [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1519453932.46.jpg [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1519453932.46.jpg ) [name] => 黄才伦 [id] => 1363857 ) [1] => Array ( [alt] => https://movie.douban.com/celebrity/1350408/ [avatars] => Array ( [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1437031126.82.jpg [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1437031126.82.jpg [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1437031126.82.jpg ) [name] => 艾伦 [id] => 1350408 ) [2] => Array ( [alt] => https://movie.douban.com/celebrity/1350407/ [avatars] => Array ( [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1437031175.04.jpg [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1437031175.04.jpg [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1437031175.04.jpg ) [name] => 宋阳 [id] => 1350407 ) ) [collect_count] => 112833 [original_title] => 李茶的姑妈 [subtype] => movie [directors] => Array ( [0] => Array ( [alt] => https://movie.douban.com/celebrity/1313050/ [avatars] => Array ( [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1518587170.5.jpg [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1518587170.5.jpg [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1518587170.5.jpg ) [name] => 吴昱翰 [id] => 1313050 ) ) [year] => 2018 [images] => Array ( [small] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2533384240.jpg [large] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2533384240.jpg [medium] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2533384240.jpg ) [alt] => https://movie.douban.com/subject/27092785/ [id] => 27092785 ) ) [title] => 正在上映的电影-邯郸 ) curl_getinfo函数获得信息: =============================================curl获得慕课用户信息================================================= <?php //C:\phpStudy\PHPTutorial\WWW\curl date_default_timezone_set('PRC'); $data="username=用户名&password=密码&remember=1"; //15510485900 $httpHeader=["Content-Type: application/x-www-form-urlencoded; charset=utf-8","Content-length:".strlen($data)]; $obj=curl_init(); curl_setopt($obj,CURLOPT_URL,"https://www.imooc.com/user/login"); //curl_setopt($obj,CURLOPT_URL,"https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.19)"); curl_setopt($obj,CURLOPT_RETURNTRANSFER,1); curl_setopt($obj,CURLOPT_COOKIESESSION,true); curl_setopt($obj,CURLOPT_COOKIEFILE,'cookiefile'); curl_setopt($obj,CURLOPT_COOKIEJAR,'cookiefile'); curl_setopt($obj,CURLOPT_COOKIE,session_name().'='.session_id()); curl_setopt($obj,CURLOPT_HEADER,0); curl_setopt($obj,CURLOPT_FOLLOWLOCATION,1); curl_setopt($obj,CURLOPT_SSL_VERIFYPEER,false);//不需要认证 curl_setopt($obj,CURLOPT_POST,1); curl_setopt($obj,CURLOPT_POSTFIELDS,$data); curl_setopt($obj,CURLOPT_HTTPHEADER,$httpHeader); curl_setopt($obj, CURLOPT_TIMEOUT, 300); $return=curl_exec($obj); if($return===false){ var_dump(curl_error($obj)); }else{ var_dump(curl_error($obj)); var_dump(curl_errno($obj)); } //echo $return; //curl_close($obj); //echo 'ceshi '; //die; curl_setopt($obj,CURLOPT_URL,"https://www.imooc.com/u/5510462/courses"); //curl_setopt($obj,CURLOPT |
2022-08-17
2022-11-06
2022-07-30
2022-08-17
2022-08-15
请发表评论