PHP - Automate User Backup

Not Yet Reviewed

Here's my script that can capture the user details with devices (except mobile app).
The script looks unprofessional but the function is working.

Things needed.
1. Any OS that can run php command.
2. Any OS that can create scheduled task/Crontab
3. Create a file and copy the scripts below.
4. Create a scheduled tasks that run below script.
5. Add a script to send an email and attach the file once the php script finishes running.

Code is on comment

0

Comments

2 comments
Date Votes

Please sign in to leave a comment.

  • <?php

    $endpoint = "https://instance.xmatters.com/api/services/xmatters-5.5.47.xmatters-5.5.47HttpSoap12Endpoint/";

    $request = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"
    xmlns:sch=\"http://www.xmatters.com/webservices/schema#5.5.47\">\r\n
    <soap:Header/>\r\n
    <soap:Body>\r\n
    <sch:FindPersons>\r\n
    <sch:user>username</sch:user>\r\n
    <sch:password>password</sch:password>\r\n
    <sch:clientTimestamp></sch:clientTimestamp>\r\n
    <sch:clientIP></sch:clientIP>\r\n
    <sch:clientOSUser></sch:clientOSUser>\r\n
    <sch:company></sch:company>\r\n
    <sch:searchParameters>\r\n
    <sch:roleName></sch:roleName>\r\n
    <sch:targetName></sch:targetName>\r\n
    </sch:searchParameters>\r\n
    </sch:FindPersons>\r\n
    </soap:Body>\r\n</soap:Envelope>";

    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => $endpoint,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $request,
    CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: text/plain"
    ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);

    $dom = new DomDocument();
    $dom -> loadXML($response);
    $x = 0;

    $heading = "User,First Name,Last Name,Work Email,Work Email Status,Work Email Valid,Work Email 2,Work Email 2 Status,Work Email 2 Valid,SMS Phone,SMS Phone Status,SMS Phone Valid,SMS Phone 2,SMS Phone 2 Status,SMS Phone 2 Valid,SMS Phone 3,SMS Phone 3 Status,SMS Phone 3 Valid,Work Phone,Work Phone Status,Work Phone Valid,Home Phone,Home Phone Status,Home Phone Valid,Mobile Phone,Mobile Phone Status,Mobile Phone Valid,Pager,Pager Status,Pager Valid \r\n";

    //echo $heading;

    $file = 'user_export.csv';
    file_put_contents($file, $heading);

    while(is_object($targetName = $dom->getElementsByTagName('targetName')->item($x)))
    {

    $targetName = $dom->getElementsByTagName('targetName')->item($x)->textContent;
    $firstName = $dom->getElementsByTagName('firstName')->item($x)->textContent;
    $lastName = $dom->getElementsByTagName('lastName')->item($x)->textContent;


    $request1 = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"
    xmlns:sch=\"http://www.xmatters.com/webservices/schema#5.5.47\">\r\n
    <soap:Header/>\r\n <soap:Body>\r\n
    <sch:FindDevices>\r\n
    <sch:user>username</sch:user>\r\n
    <sch:password>password</sch:password>\r\n
    <sch:clientTimestamp></sch:clientTimestamp>\r\n
    <sch:clientIP></sch:clientIP>\r\n
    <sch:clientOSUser></sch:clientOSUser>\r\n
    <sch:company></sch:company>\r\n
    <sch:searchParameters>\r\n
    <sch:owner>".$targetName."</sch:owner>\r\n
    </sch:searchParameters>\r\n
    </sch:FindDevices>\r\n
    </soap:Body>\r\n</soap:Envelope>";

    $curl1 = curl_init();
    curl_setopt_array($curl1, array(
    CURLOPT_URL => $endpoint,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $request1,
    CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: text/plain"
    ),
    ));

    $response1 = curl_exec($curl1);

    curl_close($curl1);

    $doc = new DomDocument();
    $doc -> loadXML($response1);

    $emailDevice = $doc->getElementsByTagName( "emailDevice" )->item(0);
    $emailDevice1 = $doc->getElementsByTagName( "emailDevice" )->item(1);
    $textPhoneDevice = $doc->getElementsByTagName( "textPhoneDevice" )->item(0);
    $textPhoneDevice1 = $doc->getElementsByTagName( "textPhoneDevice" )->item(1);
    $textPhoneDevice2 = $doc->getElementsByTagName( "textPhoneDevice" )->item(2);
    $voiceDevice = $doc->getElementsByTagName( "voiceDevice" )->item(0);
    $voiceDevice1 = $doc->getElementsByTagName( "voiceDevice" )->item(1);
    $voiceDevice2 = $doc->getElementsByTagName( "voiceDevice" )->item(2);
    $textPagerDevice = $doc->getElementsByTagName( "textPagerDevice" )->item(0);

    if (!is_object($emailDevice)) {
    $emailDevice_address = "";
    $emailDevice_active = "";
    $emailDevice_testStatus = "";

    } else {

    $emailDevice_address = $emailDevice->getElementsByTagName( "address" )->item(0)->textContent;
    $emailDevice_active = $emailDevice->getElementsByTagName( "active" )->item(0)->textContent;
    $emailDevice_testStatus = $emailDevice->getElementsByTagName( "testStatus" )->item(0)->textContent;

    }

    if (!is_object($emailDevice1)) {
    $emailDevice_address1 = "";
    $emailDevice_active1 = "";
    $emailDevice_testStatus1 = "";

    } else {
    $emailDevice_address1 = $emailDevice1->getElementsByTagName( "address" )->item(0)->textContent;
    $emailDevice_active1 = $emailDevice1->getElementsByTagName( "active" )->item(0)->textContent;
    $emailDevice_testStatus1 = $emailDevice1->getElementsByTagName( "testStatus" )->item(0)->textContent;
    }

    if (!is_object($textPhoneDevice)) {
    $textPhoneDevice_countryCode = "";
    $textPhoneDevice_number = "";
    $textPhoneDevice_active = "";
    $textPhoneDevice_testStatus = "";

    } else {

    $textPhoneDevice_countryCode = $textPhoneDevice->getElementsByTagName( "countryCode" )->item(0)->textContent;
    $textPhoneDevice_number = $textPhoneDevice->getElementsByTagName( "number" )->item(0)->textContent;
    $textPhoneDevice_active = $textPhoneDevice->getElementsByTagName( "active" )->item(0)->textContent;
    $textPhoneDevice_testStatus = $textPhoneDevice->getElementsByTagName( "testStatus" )->item(0)->textContent;

    }

    if (!is_object($textPhoneDevice1)) {
    $textPhoneDevice_countryCode1 = "";
    $textPhoneDevice_number1 = "";
    $textPhoneDevice_active1 = "";
    $textPhoneDevice_testStatus1 = "";

    } else {

    $textPhoneDevice_countryCode1 = $textPhoneDevice1->getElementsByTagName( "countryCode" )->item(0)->textContent;
    $textPhoneDevice_number1 = $textPhoneDevice1->getElementsByTagName( "number" )->item(0)->textContent;
    $textPhoneDevice_active1 = $textPhoneDevice1->getElementsByTagName( "active" )->item(0)->textContent;
    $textPhoneDevice_testStatus1 = $textPhoneDevice1->getElementsByTagName( "testStatus" )->item(0)->textContent;

    }



    if (!is_object($textPhoneDevice2)) {
    $textPhoneDevice_countryCode2 = "";
    $textPhoneDevice_number2 = "";
    $textPhoneDevice_active2 = "";
    $textPhoneDevice_testStatus2 = "";

    } else {

    $textPhoneDevice_countryCode2 = $textPhoneDevice2->getElementsByTagName( "countryCode" )->item(0)->textContent;
    $textPhoneDevice_number2 = $textPhoneDevice2->getElementsByTagName( "number" )->item(0)->textContent;
    $textPhoneDevice_active2 = $textPhoneDevice2->getElementsByTagName( "active" )->item(0)->textContent;
    $textPhoneDevice_testStatus2 = $textPhoneDevice2->getElementsByTagName( "testStatus" )->item(0)->textContent;

    }

    if (!is_object($voiceDevice)) {
    $voiceDevice_countryCodeOverride = "";
    $voiceDevice_areaCode = "";
    $voiceDevice_number = "";
    $voiceDevice_active = "";
    $voiceDevice_testStatus = "";

    } else {

    $voiceDevice_countryCodeOverride = $voiceDevice->getElementsByTagName( "countryCodeOverride" )->item(0)->textContent;
    $voiceDevice_areaCode = $voiceDevice->getElementsByTagName( "areaCode" )->item(0)->textContent;
    $voiceDevice_number = $voiceDevice->getElementsByTagName( "number" )->item(0)->textContent;
    $voiceDevice_active = $voiceDevice->getElementsByTagName( "active" )->item(0)->textContent;
    $voiceDevice_testStatus = $voiceDevice->getElementsByTagName( "testStatus" )->item(0)->textContent;

    }


    if (!is_object($voiceDevice1)) {
    $voiceDevice_countryCodeOverride1 = "";
    $voiceDevice_areaCode1 = "";
    $voiceDevice_number1 = "";
    $voiceDevice_active1 = "";
    $voiceDevice_testStatus1 = "";

    } else {

    $voiceDevice_countryCodeOverride1 = $voiceDevice1->getElementsByTagName( "countryCodeOverride" )->item(0)->textContent;
    $voiceDevice_areaCode1 = $voiceDevice1->getElementsByTagName( "areaCode" )->item(0)->textContent;
    $voiceDevice_number1 = $voiceDevice1->getElementsByTagName( "number" )->item(0)->textContent;
    $voiceDevice_active1 = $voiceDevice1->getElementsByTagName( "active" )->item(0)->textContent;
    $voiceDevice_testStatus1 = $voiceDevice1->getElementsByTagName( "testStatus" )->item(0)->textContent;

    }

    if (!is_object($voiceDevice2)) {
    $voiceDevice_countryCodeOverride2 = "";
    $voiceDevice_areaCode2 = "";
    $voiceDevice_number2 = "";
    $voiceDevice_active2 = "";
    $voiceDevice_testStatus2 = "";

    } else {

    $voiceDevice_countryCodeOverride2 = $voiceDevice2->getElementsByTagName( "countryCodeOverride" )->item(0)->textContent;
    $voiceDevice_areaCode2 = $voiceDevice2->getElementsByTagName( "areaCode" )->item(0)->textContent;
    $voiceDevice_number2 = $voiceDevice2->getElementsByTagName( "number" )->item(0)->textContent;
    $voiceDevice_active2 = $voiceDevice2->getElementsByTagName( "active" )->item(0)->textContent;
    $voiceDevice_testStatus2 = $voiceDevice2->getElementsByTagName( "testStatus" )->item(0)->textContent;

    }

    if (!is_object($textPagerDevice)) {
    $textPagerDevice_pin = "";
    $textPagerDevice_name = "";
    $textPagerDevice_active = "";
    $textPagerDevice_testStatus = "";
    $textPagerDevice_pager = "";

    } else {

    $textPagerDevice_pin = $textPagerDevice->getElementsByTagName( "pin" )->item(0)->textContent;
    $textPagerDevice_name = $textPagerDevice->getElementsByTagName( "name" )->item(1)->textContent;
    $textPagerDevice_active = $textPagerDevice->getElementsByTagName( "active" )->item(0)->textContent;
    $textPagerDevice_testStatus = $textPagerDevice->getElementsByTagName( "testStatus" )->item(0)->textContent;
    $textPagerDevice_pager = $textPagerDevice_name . " - " . $textPagerDevice_pin;
    }

    $output = $targetName . "," . $firstName . "," . $lastName . "," . $emailDevice_address . "," . $emailDevice_active . "," . $emailDevice_testStatus . "," . $emailDevice_address1 . "," . $emailDevice_active1 . "," . $emailDevice_testStatus1 . "," . $textPhoneDevice_countryCode . " " . $textPhoneDevice_number . "," . $textPhoneDevice_active . "," . $textPhoneDevice_testStatus . "," . $textPhoneDevice_countryCode1 . " " . $textPhoneDevice_number1 . "," . $textPhoneDevice_active1 . "," . $textPhoneDevice_testStatus1 . "," . $textPhoneDevice_countryCode2 . " " . $textPhoneDevice_number2 . "," . $textPhoneDevice_active2 . "," . $textPhoneDevice_testStatus2 . "," . $voiceDevice_countryCodeOverride . " " . $voiceDevice_areaCode . " " . $voiceDevice_number . "," . $voiceDevice_active . "," . $voiceDevice_testStatus . "," . $voiceDevice_countryCodeOverride1 . " " . $voiceDevice_areaCode1 . " " . $voiceDevice_number1 . "," . $voiceDevice_active1 . "," . $voiceDevice_testStatus1 . "," . $voiceDevice_countryCodeOverride2 . " " . $voiceDevice_areaCode2 . " " . $voiceDevice_number2 . "," . $voiceDevice_active2 . "," . $voiceDevice_testStatus2 . "," . $textPagerDevice_pager . "," . $textPagerDevice_active . "," . $textPagerDevice_testStatus . "\r\n";

    //echo $output;

    file_put_contents($file, $output, FILE_APPEND);

    $x++;

    }


    ?>
    0
  • Great! Thanks for sharing Aris! 

    If you are looking to do anything else like this in the future, you might checkout the new REST API. The payloads are much simpler to deal with. But as always, if it works go for it!

    Happy Friday!

    0

Didn't find what you were looking for?

New post