如何将下面提到的xml数据转换为laravel

时间:2018-05-09 10:58:49

标签: laravel-5 laravel-4 laravel-5.2 laravel-5.1

在下面找到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数组并将其传递给控制器​​进行查看。

在下面找到conrtroller代码:

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');

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

1 个答案:

答案 0 :(得分:1)

只需执行以下操作即可。

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 ));

 }