<?php


namespace App\Http\helper;


use App\AdminUser;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class HttpCurl
{
    // 往中转服发通知
    public function service($url, $data)
    {
        $admin = DB::connection('write')->table('agent.dbo.admin_users')->where('id', 1)->select('account', 'password')->first();
        $account = $admin->account ?: '';
        $password = $admin->password ?: '';

        $data['accounts'] = $account;
        $data['passworld'] = $password;

        $build_query = $url . '?' . http_build_query($data);

        return HttpCurl::curl_get_shot($build_query);
    }

    function curl_getHeader($url, $header = [])
    {
        $headerArray = [];
        if (!empty($header)) {
            foreach ($header as $key => $value) {
                $headerArray[] = $key . ':' . $value;
            }
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);   //只需要设置一个秒的数量就可以
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);

        $output = mb_convert_encoding($output, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
        return $output;

    }

    function curl_get($url, $header = [])
    {
//        $headerArray = ['Content-type:application/json;", "Accept:application/json'];
        $headerArray = [
            'Content-Type: application/json;charset=UTF-8',
        ];
        if (!empty($header)) {
            foreach ($header as $key => $value) {
                $headerArray[] = $key . ':' . $value;
            }
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);   //只需要设置一个秒的数量就可以
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);

        $output = mb_convert_encoding($output, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
        return $output;

    }
    static function curl_get_shot($url, $header = [])
    {
//        $headerArray = ['Content-type:application/json;", "Accept:application/json'];
        $headerArray = [
            'Content-Type: application/json;charset=UTF-8',
        ];
        if (!empty($header)) {
            foreach ($header as $key => $value) {
                $headerArray[] = $key . ':' . $value;
            }
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);   //只需要设置一个秒的数量就可以
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);

        $output = mb_convert_encoding($output, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
        return $output;

    }


    /**
     * 传入数组进行HTTP POST请求
     * @param $url
     * @param array $post_data
     * @param int $timeout
     * @param bool $header
     * @param string $data_type
     * @return bool|string
     */
    public function curlPost($url, $post_data = array(), $data_type = "array", $header = false, $timeout = 120, $flg = false)
    {

        //支持json数据数据提交
        if ($data_type == 'json') {
            $post_string = \GuzzleHttp\json_encode($post_data);
        } elseif ($data_type == 'array') {
            $post_string = $post_data;
        } elseif (is_array($post_data)) {
            $post_string = http_build_query($post_data, '', '&');
        }

        $ch = curl_init();    // 启动一个CURL会话
        curl_setopt($ch, CURLOPT_URL, $url);     // 要访问的地址
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // 对认证证书来源的检查   // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);     // Post提交的数据包
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);     // 设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        //curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     // 获取的信息以文件流的形式返回
        if ($header) {
            $header = [
                'Content-Type: application/json',
            ];
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
        }
        $result = curl_exec($ch);

        // 获取http code
        if ($flg) {
            return curl_getinfo($ch, CURLINFO_HTTP_CODE);
        }

        curl_close($ch);
        return $result;
    }

    /**
     * 传入数组进行HTTP POST 表单请求
     * @param $url
     * @param array $data
     * @return bool|string
     */
    public function curlPostForm($url, $data = array())
    {
        //定义提交内容类型
        $headers = array('Content-Type: application/x-www-form-urlencoded');
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
        //curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            Log::info('Errno'.curl_error($curl));
        }
        curl_close($curl); // 关闭CURL会话
        return $result;
    }

    // curl-header post
    function httpsPost($url, $paramStr,$headers){
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_TIMEOUT => 120,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $paramStr,
            CURLOPT_HTTPHEADER => $headers,
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) {
            return $err;
        }
        return $response;
    }
}
