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;
    }
  • 相关阅读:
    A2-02-15.DML-MySQL RIGHT JOIN
    A2-02-14.DML- MySQL LEFT JOIN
    A2-02-13.DML- MySQL INNER JOIN
    NHibernate N+1问题实例分析和优化
    怎么创建移动页面应用程序
    .NET开发时让人头痛的SESSION超时
    WCF服务编程——数据契约快速入门
    数据模型类对比,用反射做个快乐的程序员
    javascript常见数据集
    provider:命名管道提供程序,error:40
  • 原文地址:https://www.cnblogs.com/cl94/p/9281662.html
Copyright © 2011-2022 走看看