Scrape Jobs (Get data from XML in another FTP)
<?php
// include phpseclib and get XML from iCIMs sftp
// include('Net/SFTP.php');
include('connect.php');
// External FTP credentials
// $ftp_server = "ftp.icims.com";
// $ftp_username = "vistap1111";
// $ftp_pass = "123";
$source_xml_file = "VPJobExport.xml";
//
// $sftp = new Net_SFTP($ftp_server);
// if (!$sftp->login($ftp_username, $ftp_pass)) {
// exit('Login Failed');
// }
//
// $sftp->get("$source_xml_file", "$source_xml_file");
// declare and truncate existing table
$table_name = "company_jobs";
$sql = "TRUNCATE TABLE $table_name";
if($mysqli->query($sql)) {
echo "table cleaned";
}
// parse XML and insert into database
$xmlLoad = new DOMDocument();
$xmlLoad->load($source_xml_file);
$xml = new DOMXpath($xmlLoad);
$job_counter = 0;
foreach($xml->query('//Row') as $row) {
$system_id = $xml->query("Column[@Name='System ID']", $row)->item(0)->nodeValue;
$system_id = $mysqli->escape_string($system_id);
$job_title = $xml->query("Column[@Name='External Job Title']", $row)->item(0)->nodeValue;
$job_title = $mysqli->escape_string($job_title);
$location = $xml->query("Column[@Name='Work Location : Location']", $row)->item(0)->nodeValue;
$location = $mysqli->escape_string($location);
$category = $xml->query("Column[@Name='Category']", $row)->item(0)->nodeValue;
$category = $mysqli->escape_string($category);
$job_posting = $xml->query("Column[@Name='Job Board Posting']", $row)->item(0)->nodeValue;
$job_posting = $mysqli->escape_string($job_posting);
$job_entries .= "( '$system_id', '$job_title', '$location', '$category', '$job_posting' ),";
$job_counter++;
}
$job_entries = rtrim($job_entries, ", ");
$sql = "INSERT INTO $table_name ( system_id, job_title, location, category, job_posting ) VALUES $job_entries";
if($mysqli->query($sql)) {
echo "<br />";
echo "$job_counter jobs added!";
} else {
echo $mysqli->error;
}
echo "<br />";
// echo $sql;
// log activity
$file_date = date('Y-m');
$timestamp = date('Y-m-d h:m:s');
$log_file = "_logs/{$file_date}_log";
$log = "$job_counter jobs added on $timestamp \n";
file_put_contents($log_file, $log, FILE_APPEND);
?>
<?php
// Your FTP credentials
if($_SERVER['SERVER_ADDR'] === "::1") {
$database_address = "localhost";
$database_user = "root";
$database_pass = "root";
$database_name = "test_database";
} else {
$database_address = "localhost";
$database_user = "rafaelp";
$database_pass = "1a2b3c";
$database_name = "example_db";
}
$mysqli = new mysqli($database_address,$database_user,$database_pass, $database_name);
if ($mysqli->connect_errno) {
echo "Database connection failed: " . $mysqli->connect_error;
}
?>