php curl

作者: 昱弟丶 | 来源:发表于2019-03-06 16:38 被阅读0次
        function curl($url){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5000);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4'));
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            $contents = curl_exec($ch);
            curl_close($ch);//关闭一打开的会话
            return $contents; 
        }
    

    get

    /**
     * curl Get请求函数
     *
     *
     * @url    请求的地址
     * @arr_header   请求头信息
     *
     * */
    function doGet($url,$arr_header = null){
        //初始化
        $ch = curl_init();
        /*
        保存cookie到文件中
        $cookie="cookie.txt";
    
        curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
    
        curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
        */
    
        curl_setopt($ch, CURLOPT_URL,$url);
    
        if(!empty($arr_header)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_header);
        }
    
        // 执行后不直接打印出来
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 不从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
    
        //释放curl句柄
        curl_close($ch);
    
        return $output;
    }
    

    post

    /**
     * curl post请求函数
     *
     *
     * @url    请求的地址
     * @data   传输的数据
     * @json   是否json传输
     * @arr_header   请求头信息
     *
     * */
    function http_request_post($url,$data = null,$json = false,$arr_header = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
    
            if ($json && is_array($data)) {
                $data = json_encode($data);
            }
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        if(!empty($arr_header)){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $arr_header);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        // echo curl_getinfo($curl);
        curl_close($curl);
        unset($curl);
        return $output;
    }
    

    相关文章

      网友评论

          本文标题:php curl

          本文链接:https://www.haomeiwen.com/subject/fwlxpqtx.html