从API响应问题中提取数据


Extracting data from API response issue

我一直在尝试从xml响应中提取数据,看起来像:

Array
(
    [apiversion] => 2
    [data] => Array
        (
            [0] => Array
                (
                    [basedir] => public_html/camnevbritishsweets.co.uk
                    [dir] => /home/paydayci/public_html/camnevbritishsweets.co.uk
                    [domain] => camnevbritishsweets.co.uk
                    [domainkey] => camnevbritishsweets_paydaycic2013.co.uk
                    [fullsubdomain] => camnevbritishsweets.paydaycic2013.co.uk
                    [reldir] => home:public_html/camnevbritishsweets.co.uk
                    [rootdomain] => paydaycic2013.co.uk
                    [status] => not redirected
                    [subdomain] => camnevbritishsweets
                )
            [1] => Array
                (
                    [basedir] => public_html/camnevdomains.info
                    [dir] => /home/paydayci/public_html/camnevdomains.info
                    [domain] => camnevdomains.info
                    [domainkey] => camnevdomains_paydaycic2013.co.uk
                    [fullsubdomain] => camnevdomains.paydaycic2013.co.uk
                    [reldir] => home:public_html/camnevdomains.info
                    [rootdomain] => paydaycic2013.co.uk
                    [status] => not redirected
                    [subdomain] => camnevdomains
                )
            [2] => Array
                (
                    [basedir] => public_html/cossacksoft.com
                    [dir] => /home/paydayci/public_html/cossacksoft.com
                    [domain] => cossacksoft.com
                    [domainkey] => cossacksoft.com_paydaycic2013.co.uk
                    [fullsubdomain] => cossacksoft.com.paydaycic2013.co.uk
                    [reldir] => home:public_html/cossacksoft.com
                    [rootdomain] => paydaycic2013.co.uk
                    [status] => not redirected
                    [subdomain] => cossacksoft.com
                )
            [3] => Array
                (
                    [basedir] => public_html/dog-care-advisor.com
                    [dir] => /home/paydayci/public_html/dog-care-advisor.com
                    [domain] => dog-care-advisor.com
                    [domainkey] => dog-care-advisor.com_paydaycic2013.co.uk
                    [fullsubdomain] => dog-care-advisor.com.paydaycic2013.co.uk
                    [reldir] => home:public_html/dog-care-advisor.com
                    [rootdomain] => paydaycic2013.co.uk
                    [status] => not redirected
                    [subdomain] => dog-care-advisor.com
                )
            [4] => Array
                (
                    [basedir] => public_html/dolphinroboticpoolcleaner.info
                    [dir] => /home/paydayci/public_html/dolphinroboticpoolcleaner.info
                    [domain] => dolphinroboticpoolcleaner.info
                    [domainkey] => dolphinroboticpoolcleaner.info_paydaycic2013.co.uk
                    [fullsubdomain] => dolphinroboticpoolcleaner.info.paydaycic2013.co.uk
                    [reldir] => home:public_html/dolphinroboticpoolcleaner.info
                    [rootdomain] => paydaycic2013.co.uk
                    [status] => not redirected
                    [subdomain] => dolphinroboticpoolcleaner.info
                )
            [5] => Array
                (
                    [basedir] => public_html/ff666fffff.com
                    [dir] => /home/paydayci/public_html/ff666fffff.com
                    [domain] => ff666fffff.com
                    [domainkey] => ff666fffff.com_paydaycic2013.co.uk
                    [fullsubdomain] => ff666fffff.com.paydaycic2013.co.uk
                    [reldir] => home:public_html/ff666fffff.com
                    [rootdomain] => paydaycic2013.co.uk
                    [status] => not redirected
                    [subdomain] => ff666fffff.com
                )
            [6] => Array
                (
                    [basedir] => public_html/ff8v.com
                    [dir] => /home/paydayci/public_html/ff8v.com
                    [domain] => ff8v.com
                    [domainkey] => ff8v.com_paydaycic2013.co.uk
                    [fullsubdomain] => ff8v.com.paydaycic2013.co.uk
                    [reldir] => home:public_html/ff8v.com
                    [rootdomain] => paydaycic2013.co.uk
                    [status] => not redirected
                    [subdomain] => ff8v.com
                )
            [7] => Array
                (
                    [basedir] => public_html/ff8v77.com
                    [dir] => /home/paydayci/public_html/ff8v77.com
                    [domain] => ff8v77.com
                    [domainkey] => ff8v77.com_paydaycic2013.co.uk
                    [fullsubdomain] => ff8v77.com.paydaycic2013.co.uk
                    [reldir] => home:public_html/ff8v77.com
                    [rootdomain] => paydaycic2013.co.uk
                    [status] => not redirected
                    [subdomain] => ff8v77.com
                )
        )
    [event] => Array
        (
            [result] => 1
        )
    [func] => listaddondomains
    [module] => Park
    [postevent] => Array
        (
            [result] => 1
        )
    [preevent] => Array
        (
            [result] => 1
        )
)

这只是响应的一部分,因为它太大而无法粘贴,到目前为止我所拥有的是:

  // get addon domain list
  $addons = $xmlapi->listaddondomains($cpanelU);
  // debug which prints the response
  print "<pre>"; print_r($addons); print "</pre>";
  // keep these domains
  $keep = array("ff8v77.com","dolphinroboticpoolcleaner.info");
  // loop over the domains
  foreach ($addons['data'] as $value) 
  {
        //echo $value["domain"] . "<br />"; 
        echo str_replace('​', "", $keep);
        if (in_array(trim($value["domain"]), $keep)) 
        {
           echo 'IN Array > ' . $value["domain"] . "<br />";
        } else {
           echo 'NOT IN Array > ' . $value["domain"] . "<br />";
        }   
  }

$addons是保存响应的东西,就像现在一样,它只打印第一个域X次,当它被打印到页面时,域看起来像:camnevdomains。

如果你能告诉我处理这个问题的最佳方法,我将不胜感激。

请遵循此链接,因为它显示了解析XML数据的确切方法

试试这个

您可以尝试这样做—使用字符转换函数(如mb_convert_encoding)将字符串转换为utf-8

$keep = array(
    "ff8v77.com",
    "dolphinroboticpoolcleaner.info"
);
$badchars='​';
$data = $addons['data'];
foreach( $data as $index => $arr ){
    if( is_array( $arr ) && array_key_exists( 'domain', $arr ) ){
        $domain = $arr['domain'];
        $domain = stripslashes( trim( mb_convert_encoding( $domain, 'HTML-ENTITIES', 'UTF-8' ) ) );
        if( in_array( $domain, $keep ) ){
            $keep[]=$domain;
        }
    }
}
同样值得尝试的是确保你的html有正确的元标签
<meta charset='utf-8' />
or, the older notation
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />