从API响应问题

时间:2016-10-08 06:45:12

标签: php

我一直在尝试从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.â€

任何提示都会在处理此响应的最佳方式上受到赞赏。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您可以尝试这些方法 - 使用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' />