用curl模拟http请求获取网页
来源:互联网 发布:php 整数保留2位小数 编辑:程序博客网 时间:2024/05/21 16:22
用curl模拟http请求获取网页 http://news.baidu.com/ 的内容 ,然后分析打印这个网页里面的所有链接地址
<?php/**** ** @author:hyb(76788424@qq.com)** @date:2014.05.19**/function getNews(){$url = 'http://news.baidu.com/';$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);//需要获取的URL地址curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER, 0);//启用时会将头文件的信息作为数据流输出//获取文件流$re = curl_exec($ch);//关闭curlcurl_close($ch);//转码为utf-8if($re === FALSE){ //无结果$result = null;return $result;}else{ if(json_encode($re) == 'null'){//如果编码不是utf8,则会返回null$res = iconv('GB2312','UTF-8//IGNORE',$re);}else{$res = $re;}}//正则获取超链$str = '#<a.*?href="(http://.*?)".*?>(.*?)</a>#is';preg_match_all($str,$res,$href);if(!isset($href [1] )||!isset($href [2] )){return $result = null;}else{$all_href = count($href[1]);for($i=0;$i< $all_href;$i++){$result[$i]['href'] = $href [1] [$i];if(empty($href [2] [$i])){$result[$i]['name'] = 'default';}else{$result[$i]['name'] = $href [2] [$i];}}}echo ' 共有<font color="red">'.count($href [1] ).'</font>条超链:<br/>';foreach($result as $k=>$v){echo '第'.$k.'条==><a href ='.$v['href'].' target="_blank">'.$v['name'].'-----------('.$v['href'].')</a><br/>';}return $result;}/**test**/getNews();/**tese**/?>
0 0
- 用curl模拟http请求获取网页
- curl模拟http请求
- HttpConnection模拟http请求获取网页内容
- curl 模拟http发送请求
- CURL封装的php http和https请求网页模拟post get函数
- 关于curl java 模拟http请求
- PHP curl 模拟http https请求
- php中实现curl模拟Http请求
- 【curl和wget命令】模拟HTTP请求
- 获取cURL请求的HTTP状态
- 用curl获取https请求
- 用curl获取https请求
- 模拟http请求的网页调试工具
- Linux 下curl模拟Http 的get or post请求
- Linux 下curl模拟Http 的get or post请求。
- Linux 下curl模拟Http 的get or post请求。
- Linux 下curl模拟Http 的get or post请求
- Linux 下curl模拟Http 的get or post请求
- unity3d math 常用的数学
- hdu4973(线段树)
- SpringMVC一次请求多次进入Control的原因
- poj 2983 差分约束
- Techniques for preventing overfitting in training Deep Neural Networks
- 用curl模拟http请求获取网页
- 文件操作一些总结
- IP(Internet Protocal) 地址 说明
- c++Primer学习笔记之一:程序的编译与执行
- STOMP协议说明书-1.2版
- HDU 4091Zombie’s Treasure Chest (数学加暴力)
- android手机连接PC无法正常安装驱动
- Android Annotations浅析
- 嵌入式中,自动加载sd卡脚本