php自制工具函数,常用的函数集合

字符串与爬虫相关

 

 

1,解析url

 

/**
* 模拟访问,可get可post
* @param $curl 
* @param $postInfo post数组/字符串,不填默认没有post
* @param $cookie cookie模拟
* @param $referer  来路模拟
* @param $userAgent 页数
* @param $userAgent 环境模拟
* @return $content 抓取的内容
*/
function _grab($curl,$postInfo='',$cookie='',$referer='',$userAgent=''){
     $ch = curl_init();  
     curl_setopt($ch, CURLOPT_URL, $curl);  
     //不输出头
     curl_setopt($ch, CURLOPT_HEADER, 0);   
     //以字符串返回获取的信息,不直接输出
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     //如果是https链接,不验证证书
     if(preg_match('/https/i', $curl)){
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     }
     //POST
     if($postInfo){
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$postInfo);
     }
     //加入cookie
     if($cookie){
         curl_setopt($ch,CURLOPT_COOKIE,$cookie);
     }
     //模拟来路
     if($referer){
         curl_setopt($ch, CURLOPT_REFERER, $referer);
     }
     //模拟环境
     if($userAgent){
         curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
     }
     //执行
     $content = curl_exec($ch);  
     //错误处理
     if ($content  === false) {  
       return "网络请求出错: " . curl_error($ch);  
       exit();  
     }  
     return $content;
}

 

转载于:https://www.cnblogs.com/cl94/p/9020713.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值