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"><span class="qt0">Asthma</span></content>
<content name="organizationName">National Library of Medicine</content>
<content name="altTitle">Bronchial <span class="qt0">Asthma</span></content>
<content name="FullSummary"><p><span class="qt0">Asthma</span> is a chronic disease that affects your airways. Your airways are tubes that carry air in and out of your lungs. If you have <span class="qt0">asthma</span>, 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.</p><p>Symptoms of <span class="qt0">asthma</span> include</p><ul><li>Wheezing</li><li>Coughing, especially early in the morning or at night</li><li>Chest tightness</li><li>Shortness of breath</li></ul><p>Not all people who have <span class="qt0">asthma</span> have these symptoms. Having these symptoms doesn't always mean that you have <span class="qt0">asthma</span>. Your doctor will diagnose <span class="qt0">asthma</span> based on lung function tests, your medical history, and a physical exam. You may also have allergy tests.</p><p>When your <span class="qt0">asthma</span> symptoms become worse than usual, it's called an <span class="qt0">asthma</span> attack. Severe <span class="qt0">asthma</span> attacks may require emergency care, and they can be fatal.</p><p><span class="qt0">Asthma</span> is treated with two kinds of medicines: quick-relief medicines to stop <span class="qt0">asthma</span> symptoms and long-term control medicines to prevent symptoms. </p><p>NIH: National Heart, Lung, and Blood Institute</p></content>
<content name="mesh"><span class="qt0">Asthma</span></content>
<content name="groupName">Lungs and Breathing</content>
<content name="groupName">Immune System</content>
<content name="snippet"> <span class="qt0">Asthma</span> is a chronic disease that affects your airways. Your airways are tubes that carry air in and out of your lungs. If you have <span class="qt0">asthma</span>, 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>
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