<?php
//初始化请求
$HOST = 'https://api.robotbv.com';
$method = $_SERVER['REQUEST_METHOD'];
$action = $_SERVER['REQUEST_URI'];
$streamData = file_get_contents('php://input');

//记录请求参数
file_put_contents('1.txt',var_export($_SERVER,true).PHP_EOL,FILE_APPEND);
file_put_contents('1.txt',var_export($streamData,true).PHP_EOL,FILE_APPEND);

//投注
if($action =='/zh-cn/2023/betslip'){
    $result = curl_post($HOST . $action,$streamData); 
    $data = json_decode($result,true);
    if($data['betslip']['delay']){
        $data['betslip']['delay2'] = $data['betslip']['delay'];
        $data['betslip']['delay']  = $data['betslip']['delay'] - 500;
        $result = json_encode($data,JSON_UNESCAPED_UNICODE);
    }
}
//延时投注
else if($action =='/zh-cn/2023/betslip/save-delayed'){
    $result = curl_post($HOST . $action,$streamData);
    //exit($streamData);
}
//其他请求中转
else if($method =='GET'){ 
    $result = curl_get($HOST . $action,$params);
}
else if($method =='POST'){
    $result = curl_post($HOST . $action,$streamData);
}

//转发请求结果
exit($result);
 
// 模拟GET请求
function curl_get($url, $params = array()) {
    $get = $_GET;
    if(!empty($params)){
        if(isset($get['s']))unset($get['s']);
        $query = http_build_query($params);
        $url = $url . '?' . $query; 
    }
 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 如果不需要验证SSL证书，可以加上这行
    
    $header = getHeader();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
 
    $response = curl_exec($ch);
    curl_close($ch);
 
    file_put_contents('2.txt',  $url .  PHP_EOL . var_export($header,true).PHP_EOL . var_export($response,true).PHP_EOL,FILE_APPEND);
    return $response;
}
 
// 模拟POST请求
function curl_post($url, $post_data='') {
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 如果不需要验证SSL证书，可以加上这行
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    
    $header = getHeader($post_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    file_put_contents('2.txt',  $full_url .  PHP_EOL . var_export($header,true).PHP_EOL,FILE_APPEND);
 
    $response = curl_exec($ch);
    curl_close($ch);
 
    //file_put_contents('2.txt',   var_export($response,true).PHP_EOL,FILE_APPEND);
    return $response;
}
 
 
//处理标头 HTTP_USER_AGENT => User-Agent
function ucfirsts($str) {
    $strs = explode('_',$str);
    $newStrs = [];
    foreach ($strs as $k => $val){
        if($val=='HTTP' && $k==0)
            continue;
        if(strlen($val)>2)
            $newStrs[] = ucfirst(strtolower($val));
        else    
            $newStrs[] = $val;
    } 
    return implode('-',$newStrs);
}

//构造header 
function getHeader($post_data = "") {
    
    $header = array();
    //过滤特殊的字段
    $filters = ['HTTP_HOST','HTTP_CONTENT_LENGTH','HTTP_CONNECTION','HTTP_ACCEPT_ENCODING'];
    foreach ($_SERVER as $key =>$val){
        if(strpos($key,'HTTP_')!==FALSE && !in_array($key,$filters)){
            $key2 = ucfirsts($key);
            $header[] = $key2 .': ' .$val;
        }
    }
    /*
    $header = array(
        'User-Agent: ' . $_SERVER['HTTP_USER_AGENT'],
        'X-Device-Identifier: ' . $_SERVER['HTTP_X_DEVICE_IDENTIFIER'],
        'X-BV-DeviceOSVersion: '. $_SERVER['HTTP_X_BV_DEVICEOSVERSION'],
        'X-BV-AppName: '. $_SERVER['HTTP_X_BV_APPNAME'],
        'X-BV-AppVersion:'. $_SERVER['HTTP_X_BV_APPVERSION'],
        'X-BV-DeviceType: '. $_SERVER['HTTP_X_BV_DEVICETYPE'],
        'X-BV-TimeZone: '. $_SERVER['HTTP_X_BV_TIMEZONE'],
        'X-BV-DeviceOS: '. $_SERVER['HTTP_X_BV_DEVICEOS'],
        'X-BV-Scale: '. $_SERVER['HTTP_X_BV_SCALE'],
        'X-BV-Licence: '. $_SERVER['HTTP_X_BV_LICENCE'],
    );
    //兼容融入authorization
    if(isset($_SERVER['HTTP_AUTHORIZATION'])){
        $authorization =$_SERVER['HTTP_AUTHORIZATION'];
        $header[] = 'Max-Retry-Count: 1';
        $header[] = 'Authorization: '. $authorization;
    }
    if($post_data){
        if(isset($_SERVER['CONTENT_TYPE'])){
            $header[] = 'Content-Type:'.$_SERVER['CONTENT_TYPE'];
        }
        else $header[] = 'Content-Type: application/json; charset=UTF-8';
    }*/
    
    file_put_contents('7.txt',  $url .  PHP_EOL . var_export($header,true).PHP_EOL ,FILE_APPEND);
    return $header;
}
