private function doTheConnectSiteWorx($api_controller, $email, $password, $domain, $action, array $input = NULL)
 {
     $key = array('email' => $email, 'password' => $password, 'domain' => $domain);
     $client = new SoapClient("{$this->http}://{$this->hostname}:{$this->port}/soap?wsdl");
     $result = $client->route($key, $api_controller, $action, $input);
     return $result;
 }
function refresh_cache($employee_id, $client_id, $site_id, $tg_name, $table_name, $questions = array(), $qids = '', $truncate = false, $current_page = 1, $see_response = false, $url)
{
    /* XML */
    $page = $current_page;
    include "XML/xml_request.php";
    $instance = $url;
    try {
        $client = new SoapClient($instance, array('soap_version' => SOAP_1_2));
    } catch (Exception $e) {
        if ($e) {
            return array("error_order" => "loop", "response" => 0, "message" => "API error. TG: " . $tg_name . ", Site id: " . $site_id . ", on page:" . $current_page, "employee_id" => $employee_id, "client_id" => $client_id, "site_id" => $site_id, "tg_name" => $tg_name, "table_name" => $table_name, "questions" => $questions, "truncate" => 0, "page" => $current_page, "qids" => $qids, "soap_url" => $url);
        }
    }
    try {
        $api_response = $client->route(array('inputXml' => $xml));
    } catch (Exception $e) {
        //print_r($e);
    }
    $xml_result = $api_response->routeResult;
    if ($see_response == true) {
        /* KILLS THE SCRIPT IF $_GET['see_response'] == 'yes' */
        print_r($xml_result);
        die;
    }
    /****************XML parsing****************/
    $result_array = json_decode(json_encode((array) simplexml_load_string($xml_result)), 1);
    /****************Check if request is successful****************/
    /*
     *Code 200 = Success 
     *Code 405 = Fail
     *If request fails the function will return FALSE
     */
    $status = isset($result_array["Status"]["Code"]) ? $result_array["Status"]["Code"] : 'no API response';
    if ($status == "405") {
        //print_r($result_array);
        return array("error_order" => "loop", "response" => 0, "message" => "Request Failed - status code - " . $status . " TG:" . $tg_name . ", site id:" . $site_id, "employee_id" => $employee_id, "client_id" => $client_id, "site_id" => $site_id, "tg_name" => $tg_name, "table_name" => $table_name, "questions" => $questions, "truncate" => 0, "page" => $current_page, "qids" => $qids, "soap_url" => $url);
    }
    /****************Validate if TG has jobs****************/
    /*
     *If TG returns 0 jobs the function will return message
     */
    $total_jobs_in_tg = $result_array["Unit"]["Packet"]["Payload"]["ResultSet"]["OtherInformation"]["TotalRecordsFound"];
    if (!((int) $total_jobs_in_tg > 0)) {
        return array("error_order" => "loop", "response" => 0, "message" => "Request found 0 jobs. TG: " . $tg_name . ", site id:" . $site_id . " on page:" . $current_page, "employee_id" => $employee_id, "client_id" => $client_id, "site_id" => $site_id, "tg_name" => $tg_name, "table_name" => $table_name, "questions" => $questions, "truncate" => 0, "page" => $current_page, "qids" => $qids, "soap_url" => $url);
    }
    /****************Make INSERT SQL statement****************/
    $columns_names_string = "";
    $values_to_insert = "";
    //print_r($result_array); die;
    if ((int) $total_jobs_in_tg == 1) {
        $data_set = $result_array["Unit"]["Packet"]["Payload"]["ResultSet"]["Jobs"];
    } else {
        $data_set = $result_array["Unit"]["Packet"]["Payload"]["ResultSet"]["Jobs"]["Job"];
    }
    /*This array will collect the SQL insert statements for each req*/
    $all_sql_statements = array();
    //print_r($data_set); die;
    foreach ($data_set as $job_data) {
        $columns_names_string .= "`job_cache_id`, ";
        $columns_names_string .= "`tg_employee_id`, ";
        $columns_names_string .= "`tg_client_id`, ";
        $columns_names_string .= "`tg_site_id`, ";
        $columns_names_string .= isset($job_data["HotJob"]) ? "`job_hot_job`, " : "";
        $columns_names_string .= isset($job_data["JobDetailLink"]) ? "`job_link`, " : "";
        $columns_names_string .= isset($job_data["JobDescription"]) ? "`job_description`, " : "";
        $columns_names_string .= isset($job_data["LastUpdated"]) ? "`job_last_updated`, " : "";
        /*questions*/
        if (count($questions)) {
            //print_r($questions); die;
            foreach ($questions as $qid_index => $one_column_name) {
                $columns_names_string .= "`" . $one_column_name . "`, ";
            }
        }
        /*ends questions*/
        $columns_names_string .= "`job_last_cached`, ";
        $columns_names_string .= "`tg_name`";
        /*------------------------------------------------------------------------------------------------------------*/
        $values_to_insert .= "NULL, ";
        $values_to_insert .= "'" . $employee_id . "', ";
        $values_to_insert .= "'" . $client_id . "', ";
        $values_to_insert .= "'" . $site_id . "', ";
        //print_r($values_to_insert); die;
        $values_to_insert .= isset($job_data["HotJob"]) ? "'" . $job_data["HotJob"] . "'," : "";
        $values_to_insert .= isset($job_data["JobDetailLink"]) ? "'" . $job_data["JobDetailLink"] . "'," : "";
        $values_to_insert .= isset($job_data["JobDescription"]) ? "'" . addslashes($job_data["JobDescription"]) . "'," : "";
        $values_to_insert .= isset($job_data["LastUpdated"]) ? "'" . $job_data["LastUpdated"] . "' ," : "";
        if (isset($result_array["Unit"]["Packet"]["Payload"]["ResultSet"]["Jobs"]["Job"]["Question"])) {
            $more_than_one_job = 0;
        }
        if (isset($job_data["Question"])) {
            $more_than_one_job = 1;
        }
        /*questions*/
        if (count($questions)) {
            $jobs_pack = NULL;
            if (!$more_than_one_job) {
                @($jobs_pack = $result_array["Unit"]["Packet"]["Payload"]["ResultSet"]["Jobs"]["Job"]["Question"]);
            } else {
                @($jobs_pack = $job_data["Question"]);
            }
            //print_r($jobs_pack); die;
            //$opco_name = '';
            //print_r($jobs_pack); die;
            foreach (@$jobs_pack as $job_index => $question_value) {
                if (!is_array($question_value)) {
                    /* This if validates the array structure */
                    @($values_to_insert .= "'" . addslashes($question_value) . "', ");
                } else {
                    @($values_to_insert .= "'', ");
                }
            }
        }
        /*ends questions*/
        $values_to_insert .= "'" . date('Y-m-d H:i:s') . "',";
        $values_to_insert .= "'" . trim($tg_name) . "'";
        $sql_insert_statement = "INSERT INTO `" . $table_name . "` (" . $columns_names_string . ") VALUES (" . $values_to_insert . ");";
        //print_r($sql_insert_statement); die;
        $all_sql_statements[] = $sql_insert_statement;
        /*Reset variables*/
        $columns_names_string = "";
        $values_to_insert = "";
    }
    /****************Insert jobs into database***************/
    $connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if ($truncate) {
        /* In case truncating the table fails, we truncate it agin  */
        if (!$connection->query("TRUNCATE " . $table_name)) {
            $connection->query("TRUNCATE " . $table_name);
        }
    }
    $insert_count = 0;
    $insert_errors = array();
    foreach ($all_sql_statements as $one_insert) {
        if ($connection->query($one_insert)) {
            $insert_count++;
        } else {
            $insert_errors[] = $one_insert;
        }
    }
    $all_sql_statements = array();
    $plural = $insert_count > 1 ? "jobs were" : "job was";
    return array("response" => TRUE, "message" => "Success - " . $insert_count . " " . $plural . " cached. Talent Gateway: " . $tg_name . " into table " . $table_name, "failed inserts" => $insert_errors);
}