<?php
/**
 * Created by PhpStorm.
 * User: hasee
 * Date: 2017/4/17
 * Time: 16:33
 */
function  curl($url,$method,$data=array(),$token="",$setcooke=false,$cookie_file='1.txt'){
    $ch = curl_init();	 //1.初始化
    //设置请求头信息
    $headers = array();
    $headers[] ="Content-type:application/json";
    $headers[] = "Authorization: Bearer ".$token;
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);


    curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
    //4.参数如下	绕过服务器端SSL的验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    //伪装请求来源，绕过防盗
    //curl_setopt($ch,CURLOPT_REFERER,"http://wthrcdn.etouch.cn/");
    //curl_setopt($ch,CURLOPT_REFERER,"http://upload1.techweb.com.cn");

    //配置curl解压缩方式（默认的压缩方式）
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
    //curl_setopt($ch, CURLOPT_ENCODING, "gzip");
    //配置代理
    // curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0'); //指明以哪种方式进行访问
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //0代表直接输出内容，1代表把内容保存在变量
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);

    if($method=="POST"){//5.post方式的时候添加数据
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    if($method=="PUT"){//5.put方式的时候添加数据
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    //模拟登陆
    if($setcooke==true){
        //如果设置要请求的cookie，那么把cookie值保存在指定的文件中
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    }else{
        //就从文件中读取cookie的信息,并验证
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    }
    //不直接输出内容
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //执行
    $tmpInfo = curl_exec($ch);

    if (curl_errno($ch)) {
        return curl_error($ch);
    }
    //释放资源
    curl_close($ch);
    //返回获取的信息
    return $tmpInfo;
}

function post($url,$data)
{
    $postdata = http_build_query(
        $data
    );

    $opts = array('http' =>
                array(
                    'method' => 'POST',
                    'header' => 'Content-type: application/x-www-form-urlencoded',
                    'content' => $postdata
                )
    );
    $context = stream_context_create($opts);
    $result = file_get_contents($url,false,$context);
    return $result;
}

function curl_post($url,$data)
{
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 1);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);

    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    //执行命令
    $result = curl_exec($curl);

    //关闭URL请求
    curl_close($curl);
    //显示获得的数据
    return $result;

}

function send_request($url, $data, $method = 'GET', $contentType = 'application/json', $refererUrl = '', $timeout = 30, $proxy = false) {
    $ch = null;
    if('POST' === strtoupper($method)) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER,0 );
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        if ($refererUrl) {
            curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
        }
        if($contentType) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
        }
        if(is_string($data)){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        } else {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        }
    } else if('GET' === strtoupper($method)) {
        if(is_string($data)) {
            $real_url = $url. (strpos($url, '?') === false ? '?' : ''). $data;
        } else {
            $real_url = $url. (strpos($url, '?') === false ? '?' : ''). http_build_query($data);
        }

        $ch = curl_init($real_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        if ($refererUrl) {
            curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
        }
    } else {
        $args = func_get_args();
        return false;
    }

    if($proxy) {
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
    }
    $ret = curl_exec($ch);
    $info = curl_getinfo($ch);
    $contents = array(
        'httpInfo' => array(
            'send' => $data,
            'url' => $url,
            'ret' => $ret,
            'http' => $info,
        )
    );

    curl_close($ch);
    return $ret;
}
