从 Laravel 中的 XML HTTP 客户端响应获取特定值
Getting specific value from XML HTTP Client Response in Laravel
我正在与域注册商通信 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 等等。如果有人能指出我的方向,那将非常有帮助。
图片具有误导性。简单XML元素中包含的全部数据未显示。
$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 进行尝试的输出
我正在与域注册商通信 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 等等。如果有人能指出我的方向,那将非常有帮助。
图片具有误导性。简单XML元素中包含的全部数据未显示。
$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 进行尝试的输出