PHP | Import staffs from a CSV file

Publié environ 1 an il y a par  Julien Pauthier

Publier un sujet
Julien Pauthier
Julien Pauthier Administrateur

You can use this script to create staffs through the API, based on a CSV file for the staff list:


<?php
$server = "app.agendize.com";
$apiKey = "";
$token = "";
$companyId = "";

$fileStaffList = "stafflist.csv";

$arrStaffEntries = explode("\n", file_get_contents($fileStaffList));
$arrRows = array();
$arrGroups = array();
$arrGroupsMembers = array();

foreach ($arrStaffEntries as $key => $row) {
    $arrRows = explode(",", $row);

    switch (trim($arrRows[0])) {
        case "Mrs":
            $title = "mrs";
        case "Mr" :
            $title = "mr";
            $lastname = strtoupper(trim($arrRows[1]));
            $firstname = ucwords(strtolower(trim($arrRows[2])));
            $email = trim($arrRows[3]);
            $phone = trim($arrRows[4]);
            $mainAddress = trim($arrRows[5]);

            echo "Creating " . $firstname . " " . $lastname . "...\n";

            $ch = curl_init("https://$server/api/2.1/scheduling/companies/$companyId/staff?apiKey=$apiKey&token=" . $token);

            //setup request to send json via POST
            $data = array(
                'firstName' => $firstname,
                'lastName' => $lastname,
                'phone' => $phone,
                'email' => $email,
                'externalId' => $email,
                'title' => $title
            );

            $payload = json_encode($data);

            curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

            //set the content type to application/json
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

            //execute the POST request
            $result = curl_exec($ch);

            //close cURL resource
            curl_close($ch);
            echo "\nDONE\n";
        break;
    }
}
?>


Here is a possible structure for the CSV file:


Title,Lastname,Firstname,Email,Phone
Mrs,DOE,Jane,jane.doe@mytestserver.com,+3378787878787


0 Votes


0 Commentaires

Connexion ou S'inscrire pour poster un commentaire