leefrom

Active Member
May 27, 2008
30
0
56
Hi Simon,

While it shouldn't be too bad to parse out the MX line, I agree it's inelegant. You might consider one of the relevant Email:: functions:

ApiEmail < ApiDocs/Api1 < TWiki

such as Email::listmxs, and then Email::delmx for deleting them nicely.
 

FrankLaszlo

Active Member
Dec 19, 2008
35
0
56
This may help. Here is my authentication function along with another function to convert the xml object to an array.

Code:
<?php

include('config.php');

function objectsIntoArray($arrObjData, $arrSkipIndices = array()) {

    $arrData = array();
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }
    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices);
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}

function cpdo($srv_ip, $whmhash, $query) {

    $myquery = "https://$srv_ip:2087/$query";
    $whmuser = 'root';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $header[0] = "Authorization: WHM $whmuser:" . $whmhash;
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_URL, $myquery);
    $r = curl_exec($curl);
    if ($r == false) {
        error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $myquery");
    }
    curl_close($curl);

    $xmlObj = simplexml_load_string($r);
    $arrResult = objectsIntoArray($xmlObj);
    return $arrResult;
}

?>
From there, you can do something like this:

Code:
        $query = "xml-api/dumpzone?domain=$domain";
        $zdata = cpdo($whmip, $whmhash, $query);
        foreach ($zdata['result']['record'] as $key => $val) {
            if ($val['type'] == "MX") {
                print "This is an MX record\n";
                print_r($val);
            }
        }
Cheers.