jeudi 24 octobre 2019

Unable to convert xml into html when passing from a laravel controller

I am making an API call where the response is an XML document. I then convert that data into a JSON array and send it to my blade.php page. When the items are printed into the website, it is acting as it is plain text and not HTML code (which is how the XML response is formatted).

I am using the GuzzleHttp package to call the API. Then i take the body of the response and convert it into a JSON array. Then pass it to the view:

$body = $response->getBody();
$xml = simplexml_load_string($body,'SimpleXMLElement');
$json = json_encode($xml);
$array = json_decode($json,TRUE);
return view('layouts.diseases.Asthma', compact("array"));

Here is what the API response looks like:

<list num="59" start="0" per="10">
    <document rank="0">
        <content name="title">&lt;span class="qt0"&gt;Asthma&lt;/span&gt;</content>
        <content name="organizationName">National Library of Medicine</content>
        <content name="altTitle">Bronchial &lt;span class="qt0"&gt;Asthma&lt;/span&gt;</content>
        <content name="FullSummary">&lt;p&gt;&lt;span class="qt0"&gt;Asthma&lt;/span&gt; is a chronic disease that affects your airways. Your airways are tubes that carry air in and out of your lungs. If you have &lt;span class="qt0"&gt;asthma&lt;/span&gt;, the inside walls of your airways become sore and swollen. That makes them very sensitive, and they may react strongly to things that you are allergic to or find irritating. When your airways react, they get narrower and your lungs get less air.&lt;/p&gt;&lt;p&gt;Symptoms of &lt;span class="qt0"&gt;asthma&lt;/span&gt; include&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Wheezing&lt;/li&gt;&lt;li&gt;Coughing, especially early in the morning or at night&lt;/li&gt;&lt;li&gt;Chest tightness&lt;/li&gt;&lt;li&gt;Shortness of breath&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Not all people who have &lt;span class="qt0"&gt;asthma&lt;/span&gt; have these symptoms. Having these symptoms doesn't always mean that you have &lt;span class="qt0"&gt;asthma&lt;/span&gt;. Your doctor will diagnose &lt;span class="qt0"&gt;asthma&lt;/span&gt; based on lung function tests, your medical history, and a physical exam. You may also have allergy tests.&lt;/p&gt;&lt;p&gt;When your &lt;span class="qt0"&gt;asthma&lt;/span&gt; symptoms become worse than usual, it's called an &lt;span class="qt0"&gt;asthma&lt;/span&gt; attack. Severe &lt;span class="qt0"&gt;asthma&lt;/span&gt; attacks may require emergency care, and they can be fatal.&lt;/p&gt;&lt;p&gt;&lt;span class="qt0"&gt;Asthma&lt;/span&gt; is treated with two kinds of medicines: quick-relief medicines to stop &lt;span class="qt0"&gt;asthma&lt;/span&gt; symptoms and long-term control medicines to prevent symptoms. &lt;/p&gt;&lt;p&gt;NIH: National Heart, Lung, and Blood Institute&lt;/p&gt;</content>
        <content name="mesh">&lt;span class="qt0"&gt;Asthma&lt;/span&gt;</content>
        <content name="groupName">Lungs and Breathing</content>
        <content name="groupName">Immune System</content>
        <content name="snippet"> &lt;span class="qt0"&gt;Asthma&lt;/span&gt; is a chronic disease that affects your airways. Your airways are tubes that carry air in and out of your lungs. If you have &lt;span class="qt0"&gt;asthma&lt;/span&gt;, the inside walls  ... </content>
    </document>

Here is the view:

<div class="container" style="height 55rem;">
    <div class="row">
        <div class="col" id="asthma_results" style="overflow-y: auto; width: 40%; height: 55rem;">
            @foreach($array["list"]["document"] as $result)
                <article id="">
                    <h2><b></b></h2>
                    <h4></h4>
                    <h4></h4>
                    <p></p>
                </article>
            @endforeach
        </div>
    </div>
</div>

Here is what i get: enter image description here

I've seen a post saying that the JSON/array conversion is the best way to send data to a page. I've read that Laravel only passes plain text to a view. When i try to return response($content)->withHeaders(['Content-Type'=>'text/XML']);(I've tried every combo of types) - I get an error

Is there another way to convert the response data to get the correct format into the view? Or is there something I can do in the view to force it to HTML?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire