<?php

$is_script = 1;
include_once __DIR__ . '/../index.php';

use app\api\model\Product;
$product_model = new Product;
$jinrui = $product_model->getOne(['id' => 4]);

//金瑞自动登陆
$login_url = 'http://pptp.zhekou5.com/index.php/admin/index/login.html';

$data = [
    'managername' => $jinrui['Account'],
    'password' => $jinrui['Pwd'],
];

$cookie = login_post($login_url, $data);

$info['Token'] = $cookie;

$product_model->updateOne(['id' => 4], $info);



//天天自动登录
$tiantian = $product_model->getOne(['id' => 14]);

$tiantian_index = "http://api.ipzuiduo.com/login";

$html = file_get_contents($tiantian_index);

preg_match('/name="_csrf-daili" value="(.*?)">            <div class="form-group has-feedback">/is', $html, $result);

$csrf_daili = $result[1];

$login_tiantian_url = 'http://api.ipzuiduo.com/login';

$data = [
    '_csrf-daili' => $csrf_daili,
    'LoginForm[username]' => $tiantian['Account'],
    'LoginForm[password]' => $tiantian['Pwd'],
];

$cookie = login_tiantian_post($login_tiantian_url, $data);
//advanced-frontend=7utomsbpg1ptm6oine5fgqsnrd; 
//_csrf-daili=ca85579e495db7209219a2ece2ab31b104902e3b52207368c36e970c184b31dca%3A2%3A%7Bi%3
//A0%3Bs%3A11%3A%22_csrf-daili%22%3Bi%3A1%3Bs%3A32%3A%22IZPEXyCwTRuWq0ZKfyisy54y0IDcQgvw%22%3B%7D
$info['Token'] = $cookie;

$product_model->updateOne(['id' => 14], $info);

//模拟登录
function login_post($url, $post)
{
    $cookie = '';
    $curl = curl_init(); //初始化curl模块
    curl_setopt($curl, CURLOPT_URL, $url); //登录提交的地址
    curl_setopt($curl, CURLOPT_HEADER, 1); //是否显示头信息
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //是否自动显示返回的信息
    curl_setopt($curl, CURLOPT_POST, 1); //post方式提交
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post)); //要提交的信息
    $result = curl_exec($curl);
    // var_dump($result);
    preg_match_all('/set\-cookie:([^\r\n]*); path/i', $result, $m);
    $cookie = trim($m[1][0] . ';' . $m[1][1] . ';');
    // var_dump($cookie);
    curl_close($curl); //关闭cURL资源，并且释放系统资源
    return $cookie;
}

//模拟登录
function login_tiantian_post($url, $post, $header='')
{
    $cookie = '';
    $curl = curl_init(); //初始化curl模块
    curl_setopt($curl, CURLOPT_URL, $url); //登录提交的地址
    curl_setopt($curl, CURLOPT_HEADER, 1); //是否显示头信息
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //是否自动显示返回的信息
    curl_setopt($curl, CURLOPT_POST, 1); //post方式提交
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post)); //要提交的信息
    $result = curl_exec($curl);
    preg_match_all('/set\-cookie:([^\r\n]*); path/i', $result, $m);
    $cookie = trim($m[1][0] . ';' . $m[1][2] . ';');
    // var_dump($cookie);
    curl_close($curl); //关闭cURL资源，并且释放系统资源
    return $cookie;
}
