为什么一段时间后JSON文件变空?

时间:2019-04-19 16:16:42

标签: php json api php-curl podio

我正在使用cron作业将联系人从属性软件复制到Podio,并且已经将联系人复制到contacts.json文件中以减少Podio API调用。但是,复制2000+个联系人后,我的JSON文件为空,并且API从开始或第一个联系人开始处理。联系人正在被复制,每个联系人约有200个。

我找不到原因,为什么一段时间后我的文件为空?

这是代码

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");

}

0 个答案:

没有答案