<?php
class vinx
{
    static function ping($host, $port, $timeout) { 
        $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
        if (!$fP) { return false; } 
        return true;
    }
    static function query($vin){
        ppm:{
            $opts = array('http' =>
                array(
                    'method'  => 'GET',
                    'header'  => "x-api-key: pGM7nKb8AGp0AGx6AmHmZQx7AJj5rJjkGxR7BQL5BQVjZwWlMGL7GxuUDxMVHD==\r\nauthorization: Bearer ".file_get_contents(implode("/",array_slice(explode("/",realpath(".")), 0, -1))."/vinx.txt")
                )
            );
            $context  = stream_context_create($opts);
            $result = file_get_contents('https://multibrand.servicebox-parts.com/bo/vehicle/v1/api/vehicles/vin?vin='.$vin, false, $context);

        }
        if(!$result&&false){
            if(!self::ping("41.226.255.80",8085, 5)){/* mail("jokhml@gmail.com","V2".date("Y-m-d h:i:s a"),"V2".date("Y-m-d h:i:s a")); */return null;};
            $uu=file_get_contents("http://41.226.255.80:8085/box3.php", 0, stream_context_create(["http"=>["timeout"=>50]]));
            if(strlen($uu)>1560&&strlen($uu)<2000){
                file_put_contents(implode("/",array_slice(explode("/",realpath(".")), 0, -1)).'/vinx.txt',$uu);
                sleep(2);
                goto ppm;
            }
        }
        $rep = json_decode($result,true);
        foreach ($rep as $key => $value){
            foreach ($value as $keyx => $valuex){
                if(is_array($valuex)){
                    $rep[$key][$keyx]=implode(" | ",$valuex);
                }
                if(($keyx=="yearOfConstrFrom"||$keyx=="yearOfConstrTo")&&$valuex){
                    $rep[$key][$keyx]=date('Y.m', $valuex/1000);
                }
            }
        }
		return $rep;
    }
}