zoukankan      html  css  js  c++  java
  • curl模板----php发送post,get请求

    function _grab($curl,$ip='',$referer='',$postInfo='',$cookie=''){
         $ch = curl_init();  
         curl_setopt($ch, CURLOPT_URL, $curl);  
         //不输出头
         curl_setopt($ch, CURLOPT_HEADER, 0);   
         //以字符串返回获取的信息,不直接输出
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    
         //SOCKS代理
         if($ip){
            curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);  //代理认证模式  
            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
            $string_arr = explode(":", $ip);
            curl_setopt($ch, CURLOPT_PROXY, $string_arr[0]);
            curl_setopt($ch, CURLOPT_PROXYPORT, $string_arr[1]);         
         }
    
    
    
         //如果是https链接,不验证证书
         if(substr_count($curl,'https')){
             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);
           }
        
           //模拟浏览器
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36');
    
         //执行
         $content = curl_exec($ch);  
         //错误处理
         if ($content  === false) {  
           return "网络请求出错: " . curl_error($ch);  
           exit();  
         }  
         return $content;
    }

    //php返回json

    echo json_encode($data);

    //php将返回的json转换成数组

    json_decode($data,true);

    //爬虫
    function _grab($curl,$postInfo='',$cookie='',$referer=''){
         $ch = curl_init();  
         curl_setopt($ch, CURLOPT_URL, $curl);  
         //不输出头
         curl_setopt($ch, CURLOPT_HEADER, 0);   
         //以字符串返回获取的信息,不直接输出
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
         //如果是https链接,不验证证书
         if(substr_count($curl,'https')){
             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);
         }
        
           //模拟浏览器
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36');
    
         //执行
         $content = curl_exec($ch);  
         //错误处理
         if ($content  === false) {  
           return "网络请求出错: " . curl_error($ch);  
           exit();  
         }  
         return $content;
    }
  • 相关阅读:
    构建之法 读书笔记01
    团队合作第一次会议有感
    浪潮之巅 读书笔记 03
    浪潮之巅 读书笔记 02
    团队介绍
    疫情查询app 开发
    世界疫情信息爬取 及开发查询爬取数据的app
    浪潮之巅 读书笔记 01
    支付宝沙箱配置
    定时器定时执行redis操作定时器执行几次自动停止的问题
  • 原文地址:https://www.cnblogs.com/cl94/p/9281662.html
Copyright © 2011-2022 走看看