【问题标题】:How to convert the below mentioned xml data into json array in laravel如何将下面提到的xml数据转换为laravel中的json数组
【发布时间】:2018-05-10 14:19:41
【问题描述】:

在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数组,然后从控制器传给view。

在控制器代码下方找到:

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

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

【问题讨论】:

  • 究竟是什么不起作用?你得到了什么错误?
  • 这是我得到的错误:“htmlspecialchars() 期望参数 1 是字符串,给定数组”
  • 你又问了同样的问题,你应该拒绝接受的答案,只在这里继续讨论stackoverflow.com/questions/50270240/…

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


【解决方案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 ));

 }

【讨论】:

  • 谢谢 很有用。请帮助我如何解析这个 json 数据并将其发送到我的视图。因为当我再次尝试解码这个 json 时,我遇到了同样的错误。
  • 如果您对其进行解码,则无法在视图中显示整个{{$reponse}}。您需要指定特定属性,例如 {{$response['Command]}}
猜你喜欢
  • 2018-10-20
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 2016-07-03
  • 2018-04-16
相关资源
最近更新 更多