RPeraltaJr
5/30/2017 - 6:16 PM

Send XML to Tenstreet

Send XML to Tenstreet

<?php
  include_once("includes/connect.php");
  
  // set date/time
  // the date/time will display in the format: M/D/YYYY H:MM:SS AM/PM
  date_default_timezone_set("America/New_York");
  $tenstreetDateTime = date("m/d/Y g:i:s A");
  
  // set tablename variable
  $tablename = "submissions";
  
  // sometimes records get stuck in a 'posting_now' post_status
  // change those records back to 'pending' so they can be processed
  $sqlPostingNow = "UPDATE $tablename SET post_status='pending' WHERE post_status='posting_now'";
  $resultPostingNow = $db->query($sqlPostingNow);
  
  // remove blank first_name apps
  $db->query("DELETE FROM $tablename WHERE post_status = 'pending' AND first_name=''");
  // remove duplicate apps
  $db->query("DELETE FROM $tablename WHERE post_status='pending' AND id NOT IN ( SELECT * FROM ( SELECT MIN(id) FROM $tablename WHERE post_status='pending' GROUP BY name, email) AS temp_table)");
  
  $sql = "select * from $tablename where post_status = 'pending' ";
  $result = $db->query($sql);
  $ia = array();    # array container to hold response results
  if ($result->num_rows == 0) {
  	$ia['msg'] = "No Pending data to submit.";
  }
  
  while ($row = $result->fetch_array()) {
  	unset($xml_data, $response_xml);
  	$sql2 = "update $tablename set post_status = 'posting_now', tenstreet_submitdatetime = '$tenstreetDateTime' where id = " . intval($row['id']);
  	$result2 = $db->query($sql2);
  
  	# build the XML you need here using $row to pull your variables and insert them into your XML.
  	$xml = new DomDocument('1.0', 'UTF-8');
  	$xml->formatOutput = true;
  
  	// TenstreetData node
  	$TenstreetData = $xml->createElement("TenstreetData");
  	$xml->appendChild($TenstreetData);
  
  	// TenstreetData > Mode node
  	$Mode = $xml->createElement("Mode", "PROD");
  	$TenstreetData->appendChild($Mode);
  
  	// TenstreetData > Source node
  	$tenstreet_source = "BayardLeadForm";
  	$Source = $xml->createElement("Source", $tenstreet_source);
  	$TenstreetData->appendChild($Source);
  
  	// TenstreetData > CompanyId node
  	$company_id = "3021";
  	$CompanyId = $xml->createElement("CompanyId", $company_id);
  	$TenstreetData->appendChild($CompanyId);
  
  	// TenstreetData > DriverId node
  	$driver_id = "1001";
  	$DriverId = $xml->createElement("DriverId", $driver_id);
  	$TenstreetData->appendChild($DriverId);
  
  	// TenstreetData > PersonalData node
  	$PersonalData = $xml->createElement('PersonalData');
  	$TenstreetData->appendChild($PersonalData);
  
  	// TenstreetData > PersonalData > PersonName node
  	$PersonName = $xml->createElement("PersonName");
  	$PersonalData->appendChild($PersonName);
  
  	// TenstreetData > PersonalData > PersonName > Prefix node
  	$Prefix = $xml->createElement("Prefix");
  	$PersonName->appendChild($Prefix);
  
  	// TenstreetData > PersonalData > PersonName > GivenName node
  	$firstname = $row['first_name'];
  	$GivenName = $xml->createElement("GivenName", $firstname);
  	$PersonName->appendChild($GivenName);
  
  	// TenstreetData > PersonalData > PersonName > FamilyName node
  	$lastname = $row['last_name'];
  	$FamilyName = $xml->createElement("FamilyName", $lastname);
  	$PersonName->appendChild($FamilyName);
  
  	// TenstreetData > PersonalData > PostalAddress node
  	$PostalAddress = $xml->createElement("PostalAddress");
  	$PersonalData->appendChild($PostalAddress);
  
  	// TenstreetData > PersonalData > PostalAddress > CountryCode node
  	$CountryCode = $xml->createElement("CountryCode", "US");
  	$PostalAddress->appendChild($CountryCode);
  
  	// TenstreetData > PersonalData > PostalAddress > Municipality node
  	$AddressCity = $row['city'];
  	$Municipality = $xml->createElement("Municipality", $AddressCity);
  	$PostalAddress->appendChild($Municipality);
  
  	// TenstreetData > PersonalData > PostalAddress > Region node
  	$AddressState = $row['state'];
  	$Region = $xml->createElement("Region", $AddressState);
  	$PostalAddress->appendChild($Region);
  
  	// TenstreetData > PersonalData > ContactData node
  	$ContactData = $xml->createElement("ContactData");
  	$contactAttribute = $xml->createAttribute('PreferredMethod');
  	$contactAttribute->value = 'PrimaryPhone';
  	$ContactData->appendChild($contactAttribute);
  	$PersonalData->appendChild($ContactData);
  
  	// TenstreetData > PersonalData > ContactData > InternetEmailAddress node
  	$email = $row['email'];
  	$InternetEmailAddress = $xml->createElement("InternetEmailAddress", $email);
  	$ContactData->appendChild($InternetEmailAddress);
  
  	// TenstreetData > PersonalData > ContactData > PrimaryPhone node
  	$PrimaryPhone = $row['phone'];
  	$PrimaryPhone = $xml->createElement("PrimaryPhone", $PrimaryPhone);
  	$ContactData->appendChild($PrimaryPhone);
  
  	// TenstreetData > ApplicationData node
  	$ApplicationData = $xml->createElement('ApplicationData');
  	$TenstreetData->appendChild($ApplicationData);
  
  	// TenstreetData > ApplicationData > AppReferrer node
  	$referrer_code = "BayardAppFeeder";
  	$AppReferrer = $xml->createElement('AppReferrer', $referrer_code);
  	$ApplicationData->appendChild($AppReferrer);
  
  	// TenstreetData > ApplicationData > DisplayFields node
  	$DisplayFields = $xml->createElement('DisplayFields');
  	$ApplicationData->appendChild($DisplayFields);
  
  	// Tenstreet non-required questions
  	$extra_questions = array(
  		"zipcode" 		=> "Zipcode",
  		"cdla_status" => "Do you have a CDL-A?",
  		"experience" 	=> "Do you have Verifiable Tractor Trailer Experience in the 10 years?",
  		"hazmat" 			=> "Do you have Hazmat Endorsement?",
  		"dui"					=> "Within the last 5 years have you: Had a DUI or been charged with reckless driving OR had license suspended for a moving violation?"
  	);
  
  	foreach($extra_questions as $id => $question) {
  		if(isset($row[$id]) && !empty($row[$id])) {
  			// TenstreetData > ApplicationData > DisplayFields > DisplayField node
  			$DisplayField = $xml->createElement('DisplayField');
  			$DisplayFields->appendChild($DisplayField);
  			// TenstreetData > ApplicationData > DisplayFields > DisplayField > DisplayId node
  			$DisplayId = $xml->createElement('DisplayId', $id);
  			$DisplayField->appendChild($DisplayId);
  			// TenstreetData > ApplicationData > DisplayFields > DisplayField > DisplayPrompt node
  			$DisplayPrompt = $xml->createElement('DisplayPrompt', $question);
  			$DisplayField->appendChild($DisplayPrompt);
  			// TenstreetData > ApplicationData > DisplayFields > DisplayField > DisplayValue node
  			$DisplayValue = $xml->createElement('DisplayValue', $row[$id]);
  			$DisplayField->appendChild($DisplayValue);
  		}
  	}
  
  	$xml_data = $xml->saveXML();
  	$xml_data_encode = utf8_encode($xml_data);
  
  	$sql3 = "UPDATE $tablename SET xml_data = '$xml_data' WHERE id = $row[id]";
  	$result3 = $db->query($sql3);
  	$post_address = "https://dashboard.tenstreet.com/post/tenstreet_standard_post_receive.php";
  	$ch = curl_init();
  	curl_setopt($ch, CURLOPT_URL, $post_address);
  	curl_setopt($ch, CURLOPT_VERBOSE, 1);
  	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  	// return into a variable curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  	// allow redirects curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8'));
  	curl_setopt($ch, CURLOPT_POST, true);
  	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  	curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data_encode);
  	$response_xml = curl_exec($ch);
  
  	//print_r($response_xml);
  	//exit;
  	// run the whole process curl_close($ch);
  	# make sure $response_xml is XML and that status says Accepted
  
  	if ((@$resp_xml_obj = simplexml_load_string(trim($response_xml))) === FALSE) {   # suppress any warnings if it fails to load...will return false nonetheless
  		# for some reason, Tenstreet's listener didn't return an XML response. This could be because of an internet outage, a site issue, etc. This almost never happens, but you should still catch the error
  		# optionally put in some retry logic and try again later.
  		continue;
  	}
  	$ia['response_subjectid'] = $db->real_escape_string($resp_xml_obj->DriverId);
  	$ia['response_result'] = $db->real_escape_string($resp_xml_obj->Status);
  	$ia['response_statusdescription'] = $db->real_escape_string($resp_xml_obj->Description);
  
  	$ia['msg'][] = "tenstreet_standard_file_poster RESPONSE RESULT IS {$ia['response_result']} SUBJECT: {$ia['response_result']} DESCRIPTION: {$ia['response_statusdescription']}";
  	$reject_reason = implode( ', ', $ia['msg']);
  
  	# Update result in table
  	if ($ia['response_result'] == "REJECTED") {
  		$sql = "UPDATE $tablename SET result = '" . $ia['response_result'] . " - " . $ia['response_statusdescription'] . "', post_status = 'rejected', tenstreet_submitdatetime = '$tenstreetDateTime' WHERE id = " . $row['id'];
  
  		//send email in case of rejected apps
  		// $subject = 'ATTN: Rejected apps for drive4fs.com';
  		// $reject_mail =  "There has been a rejected app for drive4fs.com.<br>
  		// 				Info: $reject_reason
  		// 				";
  		// $to = "";
  		// // message
  		// $message = '
  		// 		<html>
  		// 		<head>
  		// 		</head>
  		// 		<body>
  		// 		' . $reject_mail . '
  		// 		</body>
  		// 		</html>
  		// 		';
  		// $headers = 'MIME-Version: 1.0' . "\r\n";
  		// $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  		// // Additional headers
  		// $headers .= 'From:  hosting@bayardad.com' . "\r\n";
  		// mail($to, $subject, $message, $headers);
  	} else {
  		$sql = "UPDATE $tablename SET result = '{$ia['response_result']} - {$ia['response_statusdescription']}', post_status = 'complete' WHERE id = {$row['id']}";
  	}
  	$result1 = $db->query($sql);
  }
?>
<!DOCTYPE html>
<html>
	<head>
		<title>Averitt Express</title>
		<style type="text/css">
			body { background-color: #00518a; }
			* { font: normal 12px Arial; color: #fff }
		</style>
	</head>
	<body>
		<?php
		echo "<pre>";
		print_r($ia['msg']);
		?>
	</body>
</html>