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;
    }
  • 相关阅读:
    HTTP 错误 404.2
    SQL Server 2008 R2如何开启数据库的远程连接(转)
    CSS中font-family:中文字体对应的英文名称
    15/18位身份证号码正则表达式(详细版)
    C#获取系统时间及时间格式
    C#正则表达式判断输入日期格式是否正确
    Linux查看机器负载
    模拟HTTP请求超时时间设置
    MySQL show命令的用法
    innodb事务隔离级别
  • 原文地址:https://www.cnblogs.com/cl94/p/9281662.html
Copyright © 2011-2022 走看看