如何在 laravel 中将下面提到的 xml 数据转换为 json 数组



在下面找到xml数据:

<?xml version="1.0" encoding="utf-8"?> <interface-response> <Command>CHECK</Command> <APIType>API.NET</APIType> <Language>eng</Language> <ErrCount>1</ErrCount> <errors> <Err1>User not permitted from this IP address - 113.193.131.41. See http://enom.help/whitelist for details</Err1> </errors> <ResponseCount>1</ResponseCount> <responses> <response> <ResponseNumber>713254</ResponseNumber> <ResponseString>Policy error; unauthorized; user(s)</ResponseString> </response> </responses> <MinPeriod>1</MinPeriod> <MaxPeriod>10</MaxPeriod> <Server>sjl0vwapi08</Server> <Site>eNom</Site> <IsLockable>True</IsLockable> <IsRealTimeTLD>True</IsRealTimeTLD> <TimeDifference>+7.00</TimeDifference> <ExecTime>0.034</ExecTime> <Done>true</Done> <TrackingKey>5baa515b-8e29-4517-b879-05ae68e94f9a</TrackingKey> <RequestDateTime>5/9/2018 3:49:10 AM</RequestDateTime> <debug/> </interface-response>

我需要将上面的 xml 数据转换为 json 数组并将其传递给控制器查看。

在下面找到控制器代码:

public function test()
{
$response = Curl::to('https://reseller.enom.com/interface.asp?command=check&sld=decksys&tld=info&responsetype=xml&uid=userid&pw=password')->get();         

$data = simplexml_load_string($response);
$configdata   = json_encode($data);
$value = json_decode($configdata, true);
return view('clientlayout.main.test', array('response' =>$response));
}

在下面找到视图代码:

{{$response}}

路线如下:

Route::get('/test','EnomController@test');

建议解决方案解决此问题并在"我的视图"页面中打印数据。

只需执行以下操作即可。

public function test()
{
$response = file_get_contents('your_xml_url');       

$data = simplexml_load_string($response);
$configdata   = json_encode($data);
return view('clientlayout.main.test', array('response' => $configdata ));
}

相关内容

  • 没有找到相关文章

最新更新