首页 > Web开发 > 详细

php一些实用的自制方法

时间:2018-09-07 19:02:16      阅读:179      评论:0      收藏:0      [点我收藏+]

//json乱码转中文

function decodeUnicode($str){
  return preg_replace_callback(‘/\\\\u([0-9a-f]{4})/i‘,
    create_function(
      ‘$matches‘,
      ‘return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");‘
    ),
    $str);
}

 

 

curl爬虫

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;
}

 

php一些实用的自制方法

原文:https://www.cnblogs.com/cl94/p/9606524.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!