【问题标题】:Getting specific value from XML HTTP Client Response in Laravel从 Laravel 中的 XML HTTP 客户端响应中获取特定值
【发布时间】:2021-11-24 15:09:07
【问题描述】:

我正在与域注册商 API 通信,我在其中调用以获取可用的 TLD 列表。

响应在浏览器中应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<ApiResponse xmlns="http://api.namecheap.com/xml.response" Status="OK">
  <Errors />
  <RequestedCommand>namecheap.domains.getTldList</RequestedCommand>
  <CommandResponse Type="namecheap.domains.getTldList">
    <Tlds>
      <Tld Name="biz" NonRealTime="false" MinRegisterYears="1" MaxRegisterYears="10" MinRenewYears="1" MaxRenewYears="10" MinTransferYears="1" MaxTransferYears="10" IsApiRegisterable="true" IsApiRenewable="true" IsApiTransferable="false" IsEppRequired="false" IsDisableModContact="false" IsDisableWGAllot="false" IsIncludeInExtendedSearchOnly="false" SequenceNumber="5" Type="GTLD" IsSupportsIDN="false" Category="P">US Business</Tld>
      <Tld Name="bz" NonRealTime="false" MinRegisterYears="1" MaxRegisterYears="10" MinRenewYears="1" MaxRenewYears="10" MinTransferYears="1" MaxTransferYears="10" IsApiRegisterable="false" IsApiRenewable="false" IsApiTransferable="false" IsEppRequired="false" IsDisableModContact="false" IsDisableWGAllot="false" IsIncludeInExtendedSearchOnly="true" SequenceNumber="11" Type="CCTLD" IsSupportsIDN="false" Category="A">BZ Country Domain</Tld>
      <Tld Name="ca" NonRealTime="true" MinRegisterYears="1" MaxRegisterYears="10" MinRenewYears="1" MaxRenewYears="10" MinTransferYears="1" MaxTransferYears="10" IsApiRegisterable="false" IsApiRenewable="false" IsApiTransferable="false" IsEppRequired="false" IsDisableModContact="false" IsDisableWGAllot="false" IsIncludeInExtendedSearchOnly="true" SequenceNumber="7" Type="CCTLD" IsSupportsIDN="false" Category="A">Canada Country TLD</Tld>
      <Tld Name="cc" NonRealTime="false" MinRegisterYears="1" MaxRegisterYears="10" MinRenewYears="1" MaxRenewYears="10" MinTransferYears="1" MaxTransferYears="10" IsApiRegisterable="false" IsApiRenewable="false" IsApiTransferable="false" IsEppRequired="false" IsDisableModContact="false" IsDisableWGAllot="false" IsIncludeInExtendedSearchOnly="true" SequenceNumber="9" Type="CCTLD" IsSupportsIDN="false" Category="A">CC TLD</Tld>
      <Tld Name="co.uk" NonRealTime="false" MinRegisterYears="2" MaxRegisterYears="10" MinRenewYears="2" MaxRenewYears="10" MinTransferYears="2" MaxTransferYears="10" IsApiRegisterable="true" IsApiRenewable="false" IsApiTransferable="false" IsEppRequired="false" IsDisableModContact="false" IsDisableWGAllot="false" IsIncludeInExtendedSearchOnly="false" SequenceNumber="18" Type="CCTLD" IsSupportsIDN="false" Category="A">UK based domain</Tld>
    </Tlds>
  </CommandResponse>
  <Server>IMWS-A06</Server>
  <GMTTimeDifference>+5:30</GMTTimeDifference>
  <ExecutionTime>0.047</ExecutionTime>
</ApiResponse>

我是这样打电话的:

$response = Http::get(env('NAMECHEAP_SANDBOX'), [
    'ApiUser' => env('NAMECHEAP_APIUSER'),
    'ApiKey' => env('NAMECHEAP_APIKEY'),
    'UserName' => env('NAMECHEAP_USERNAME'),
    'Command' => 'namecheap.domains.gettldlist',
    'ClientIp' => env('NAMECHEAP_IP')
]);

$xml = simplexml_load_string($response->getBody(), 'SimpleXMLElement', LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json, TRUE);

我收到回复,其中 gTLD 显示如下: tld-list-image

返回数组的整体结构为:

structure-image

但是,我想检索其 API 文档中所示的简单名称列表。像 biz、cc 等。如果有人能指出我的方向,那将非常有帮助。

【问题讨论】:

    标签: php laravel simplexml


    【解决方案1】:

    图片具有误导性。未显示 SimpleXMLElement 中包含的全部数据。

    $xml = simplexml_load_string($response->getBody(), 'SimpleXMLElement', LIBXML_NOCDATA);
    
    /* I don't think this is needed
    $json = json_encode($xml);
    $array = json_decode($json, TRUE);
    */
    
    dd($xml) // This should look like your images. Notice no extra data is in the Tlds.
    
    dd($xml->CommandResponse->Tlds->Tld[0]); // surprise, the data is there.
    

    如果你想转储属性,你可以使用字符串转换来做。

    foreach ($xml->CommandResponse->Tlds->Tld as $tld) {
        dump( (string) $tld['Name'] );
    }
    

    例如,这是使用示例 XML 进行尝试的输出

    【讨论】:

    • 不客气。我添加了一种方法,您可以根据需要自行获取属性。
    • 另外,关于为什么数据没有显示,这是在php文档中:“注意:SimpleXML已经制定了在大多数方法中添加迭代属性的规则。他们无法查看使用 var_dump() 或其他任何可以检查对象的东西。"
    • 非常感谢!它在您的第一次编辑中也很有效。
    • 作为提示,您实际上并不需要LIBXML_NOCDATA 选项,它仅在将对象转回XML 时才真正相关。人们认为他们需要它的唯一原因是因为var_dump和朋友没有出现。
    猜你喜欢
    • 1970-01-01
    • 2019-06-10
    • 2015-05-05
    • 2021-02-04
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多