I'm copying contacts from propertyware to Podio using a cron job, and I've already copied contacts into the contacts.json
file to reduce Podio API calls. However, after copying 2000+ contacts my JSON file empties and API starts coping from the start or first contact. And contacts are being copied in chunks of around 200 contacts each.
I'm unable to find the reason, why does my file get empty after some time?
Here is the code
if (!file_exists(PROPERTYWARE_CONTACTS_FILE_NAME)) {
file_put_contents(PROPERTYWARE_CONTACTS_FILE_NAME, "[]");
}
$response = HttpClient(HTTP_GET, PROPERTYWARE_CONTACTS_URL);
$response = json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $response), true );
$saved_records = json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', file_get_contents(PROPERTYWARE_CONTACTS_FILE_NAME)), true );
$incoming_records = array();
$different_from_saved = array();
$old_items = array();
foreach ($response as $key => $val) {
if ($key == "records") {
$incoming_records = $val;
}
}
foreach ($incoming_records as $incoming_record) {
$add_it = true;
foreach ($saved_records as $saved_record) {
if ($saved_record == $incoming_record) {
$add_it = false;
array_push($old_items, $incoming_record);
}
}
if ($add_it) {
array_push($different_from_saved, $incoming_record);
}
}
if (count($different_from_saved) > 0) {
print "\nDifferent Data";
}
updateInPodio($different_from_saved, CONTACTS_APP_ID, $old_items);
function updateInPodio($items, $app_id, $old_items)
{
$old_items_count = count($old_items);
$new_items_count = count($items);
$pushed_into_podio = 0;
$saveToFile = [];
foreach ($old_items as $old_item) {
array_push($saveToFile, (object)$old_item);
}
try {
foreach ($items as $item) {
$phones = [];
$emails = [];
$fields = [];
$fields['propertyware'] = "YES";
if(!empty($item[0]))
$fields['entitiy-id'] = $item[0];
if(!empty($item[1]))
$fields['first-name'] = $item[1];
if(!empty($item[2]))
$fields['last-name'] = $item[2];
if(!empty($item[3]))
$fields['company'] = $item[3];
$response = PodioItem::create($app_id, ["fields" => $fields]);
$pushed_into_podio++;
array_push($saveToFile, (object)$item);
}
file_put_contents(PROPERTYWARE_CONTACTS_FILE_NAME, json_encode($saveToFile));
} catch (Exception $exception) {
file_put_contents(PROPERTYWARE_CONTACTS_FILE_NAME, json_encode($saveToFile));
print "<pre>";
print_r($exception->getMessage());
print "</pre>";
$to = "abdulhaye111@gmail.com";
$subject = "Exception While Pushing Contacts to Podio";
$message = "<br><strong>OLD_CONTACTS:</strong> $old_items_count<br><strong>NEW_CONTACTS:</strong> $new_items_count<br><strong>PUSHED_CONTACTS:</strong> $pushed_into_podio<br><br><br>".$exception->getMessage();
$header = "From:noreply@propertymanagementoh.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
}
file_put_contents(PROPERTYWARE_CONTACTS_FILE_NAME, json_encode($saveToFile));
print"<br>OLD_ITEMS: $old_items_count<br>NEW_ITEMS: $new_items_count<br>PUSHED_ITEMS: $pushed_into_podio<br>";
header("Location: http://propertymanagementoh.com/dev/building.php");
header("Location: http://propertymanagementoh.com/dev/lease.php");
header("Location: http://propertymanagementoh.com/dev/leasePayments.php");
header("Location: http://propertymanagementoh.com/dev/protfolioBills.php");
header("Location: http://propertymanagementoh.com/dev/units.php");
}
OLD_ITEMS: $old_items_count
NEW_ITEMS: $new_items_count
PUSHED_ITEMS: $pushed_into_podio
";` so right now they do nothing but generate warning messages. Please see https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – ArtisticPhoenix Apr 19 '19 at 16:45