• curl ,post,get (原创)


    curl get:

    1)直接输出

    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,"http://testopen.api.yaolan.com/api/user/reg");
    curl_exec($ch);
    curl_close($ch);

    2)curl_get函数

    function curl_get($url){                                  
            $ch=curl_init();                                  
            curl_setopt($ch,CURLOPT_URL,$url);                
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);     
            $content=curl_exec($ch);                          
            curl_close($ch);                                  
            return $content;                                  
    } 

    curl_post 函数:

    function curl_post($url,$data=array()){
            $ch=curl_init();
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
            curl_setopt($ch,CURLOPT_POST,true);//如果有下面的一行代码,这个可以不设置
            curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    
            $content=curl_exec($ch);
            curl_close($ch);
            return $content;
    }
    $data=array('uname'=>'zdctest','email'=>'zdc@yaolan.com');
    //$data=array('uname'=>'zdctest','email'=>'zdc@yaolan.com','file_name' => '@/data/lnmp/autoreg/logs/log20150415.txt');//上传文件需要加@符号  php 5.6 之后要加 curl_setopt($ch, CURLOPT_SAFE_UPLOAD,false);才能上传成功
    $url="http://testopen.api.yaolan.com/api/user/reg"; echo curl_post($url,$data);

     携带header post

        public static function  curlPost($url,$data=array(),$header=array()){
                $ch=curl_init();
                curl_setopt($ch,CURLOPT_URL,$url);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch,CURLOPT_POST,true);//如果有下面的一行代码,这个可以不设置
                curl_setopt($ch,CURLOPT_POSTFIELDS,$data);    
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);            
                empty($header) or curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                
                $content=curl_exec($ch);
                curl_close($ch);
                return $content;
        }

    -----------------------------------------新版

    curl:request

        public static function curlRequest($url,$method='post',$data=array(),$header=array()){
            $ch = curl_init(); //初始化CURL句柄 
            curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出 
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
            empty($data) or curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
            empty($header) or curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置提交header
            0===strpos($url,'https:') and curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https请求加这个代码    
            $content=curl_exec($ch);
            curl_close($ch);
            return $content;
        }

    curl post  get

    //post 函数
    function curl_post($url,$data=array(),array $header=array()){    
                $ch=curl_init();
                curl_setopt($ch,CURLOPT_URL,$url);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch,CURLOPT_POST,true);//如果有下面的一行代码,这个可以不设置
                curl_setopt($ch,CURLOPT_POSTFIELDS,$data);    
                0===strpos($url,'https:') and curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https请求加这个代码                                
                empty($header) or curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                
                $content=curl_exec($ch);
                curl_close($ch);
                return $content;
    }
    
    //get函数携带 header
    function curl_get($url,array $header=array()){
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        0===strpos($url,'https:') and curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https请求加这个代码                                
        empty($header) or curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $content=curl_exec($ch);
        curl_close($ch);
        return $content;
    }

    curlRequest:(new)

     private function curlRequest($url,$method='post',$data=array(),$header=array()){
            $ch = curl_init(); //初始化CURL句柄
            curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出
            if('post'!=strtolower($method)){
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
            }else{
                curl_setopt($ch,CURLOPT_POST,true);
            }
    
            is_array($data) and $data=http_build_query($data);
            empty($data) or curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
    
            empty($header) or curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置提交header
            0===strpos($url,'https:') and curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https请求加这个代码
            $content=curl_exec($ch);
            curl_close($ch);
            return $content;
        }
    

      

  • 相关阅读:
    C语言第三次博客作业---单层循环结构
    C语言第二次博客作业---分支结构
    C语言第一次博客作业
    第0次作业
    第09组 Beta冲刺(1/5)
    第09组 Alpha事后诸葛亮
    第09组 Alpha冲刺(6/6)
    第09组 Alpha冲刺(5/6)
    第09组 Alpha冲刺(4/6)
    第09组 Alpha冲刺(3/6)
  • 原文地址:https://www.cnblogs.com/zhudongchang/p/4429675.html
Copyright © 2020-2023  润新知