php curl抓取类分享
class UsualFunForNetWorkHelper
{
/***
* post请求数据
*/
public static function HttpsPost($url, $data = null, $headers = null)
{
$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, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
header("Content-type: text/html; charset=utf-8");
return $output;
} /***
* http的get访问方法
*/
public static function HttpGet($url, $headers = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
//curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$output = curl_exec($curl);
//echo '['.$output.']';
return $output;
} /**
* 获取图片
* @param $url
* @param $content_type
* @return bool|int|mixed
*/
public static function HttpGetImg($url,&$content_type,&$error='')
{
$content_type = '';
$count = 0;
$e_no = 28;
while($count < 10 && $e_no === 28)//网络超时,10次重复请求
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_TIMEOUT, 5); // 设置超时限制防止死循环
//curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$output = curl_exec($curl);
$content_type = curl_getinfo($curl,CURLINFO_CONTENT_TYPE); $e_no = curl_errno($curl);
curl_close($curl);
$count ++;
}
if($e_no !== 0)
{
$error = $e_no;
if($e_no === 28)
{
Log::write('请求图片超时10次,放弃,url:'.$url);
}
return false;
}
$content_type = strval($content_type);
$ok_type=[
'application/octet-stream',
'image/bmp',
'image/gif',
'image/jpeg',
'image/png'
];
if(!in_array($content_type,$ok_type))
{
return false;
}
return $output;
} }
php curl抓取类分享的更多相关文章
- 分享一个c#t的网页抓取类
using System; using System.Collections.Generic; using System.Web; using System.Text; using System.Ne ...
- PHP使用CURL抓取网页
CURL是一个非常强大的开源库,支持很多协议,包括HTTP.FTP.TELNET等,我们使用它来发送HTTP请求.它给我 们带来的好处是可以通过灵活的选项设置不同的HTTP协议参数,并且支持HTTPS ...
- php curl抓取远程页面内容的代码
使用php curl抓取远程页面内容的例子. 代码如下: <?php /** * php curl抓取远程网页内容 * edit by www.jbxue.com */ $curlPost = ...
- PHP CURL 抓取失败 自己调试
蛋疼的一下午,物理机win7旗舰版+APACHE2 ,CURL抓取一直成功. 虚拟机ubuntu+apache2一直抓取失败. 晚上,问个仁兄才知道,CURL可以调试: 参考这位兄台: 地址 curl ...
- shell用curl抓取页面乱码,参考一下2方面(转)
1.是用curl抓取的数据是用类似gzip压缩后的数据导致的乱码.乱码:curl www.1ting.com |more乱码:curl -H "Accept-Encoding: gzip&q ...
- 使用file_get_contents()和curl()抓取网络资源的效率对比
使用file_get_contents()和curl()抓取网络资源的效率对比 在将小程序用户头像合成海报的时候,用到了抓取用户头像对应的网络资源,那么抓取方式有很多,比如 file_get_cont ...
- 好用的 curl 抓取 页面的封装函数
由于经常使用php curl 抓取页面的内容,在此mark 平时自己封装的 curl函数,(其实 现在也开始用 Python 来爬了~ ^-^) /** * 封装curl方法 * @author Fr ...
- 通过CURL抓取页面中的图片路径并下载到本地
1.首页是图片处理页面downpic.php <?phpfunction getImage($url,$filename="") { if($url=="" ...
- curl抓取页面时遇到重定向的解决方法
用php的curl抓取网页遇到了问题,为阐述方便,将代码简化如下: <?php function curlGet($url) { $ch = curl_init(); curl_setopt($ ...
随机推荐
- macbook 下hadoop伪分布式安装
1 准备原材料 1.1 jdk 1.8.0_171(事先安装并配置环境变量HAVA_HOME,PATH) 1.2 Hadoop 2.8.3 2 免密登陆配置(否则安装过程需要不断输入密码) 2.1 ...
- hibernate---session查询
一.hql语句查询(适合多表) public class MyTest { public static void main(String[] args) { //查询集合 Session sessio ...
- 第1章 Java语言概述--HelloWorld--环境搭建
SE学什么 第1章 Java语言概述 第2章 基本语法 第3章 数组 第4章 面向对象编程(上) 第5章 面向对象编程(中) 第6章 面向对象编程(下) 第7章 异常处理 第8章 枚举类&注解 ...
- Hbase rowkey设计+布隆过滤器+STORE FILE & HFILE结构
Rowkey设计 Rowkey设计原则 Rowkey设计应遵循以下原则: 1.Rowkey的唯一原则 必须在设计上保证其唯一性.由于在HBase中数据存储是Key-Value形式,若HBase中同一表 ...
- python3.6 内置函数
python内置函数 # encoding: utf-8 # module builtins # from (built-in) # by generator 1.145 ""&q ...
- 【C++】读取参数的类
在C++程序中,如果我们把程序中的参数都保存在txt文本中,运行时再去读取.这样的好处是,当我们需要调参的时候,不需要每次都重新编译程序,大大提升了效率. 今日分享一份实现以上功能的代码,代码来源:h ...
- 链表(list)使用注意
如下代码是linux上的链表接口源码,使用的这个链表(list)源码,可以方便快捷的建立链表队列,但使用时需要注意的坑有: 1.不支持,多对多,否则在add的时候,因为要加入链表的对象只有一块list ...
- 协议并发测试工具 BoHexTest
BoHexTest V1.0.3 1.添加连接LOG打印2.优化代理及并发策略 大小: 1074688 字节修改时间: 2017年10月3日, 10:24:26MD5: EBAE5A17F7F5ED0 ...
- Net-Snmp工具(学习SNMP的工具,开源项目)简单使用
https://blog.csdn.net/mrzhangzifu/article/details/77882371 Net-Snmp工具的安装与配置 操作系统:Ubuntu16.4 软件版本:n ...
- ubuntu server cloud img username password
新安装了OpenStack Queens发现无镜像,蹦蹦跳跳的下载了ubuntu的镜像 网址https://cloud-images.ubuntu.com/ 最好你自己找你想要的,vmdk.ova.i ...