Home > Software engineering >  Pre-formatted HTML box losing format when retrieved from API
Pre-formatted HTML box losing format when retrieved from API

Time:06-16

I am implementing a recruitement system (Lever) for a client of ours. One of its responsibilities is to list all of the available jobs opportunities inside the client's website.

Based on their API documentation, inside the corresponding PHP file, I wrote the code to retrieve a list of all job listings and then use json_decode to parse them, before accessing each field seperately. For example, if I want to retrieve the job title for a single job opportunity, I use $jr['data'][$x]['text'] inside a for loop, where $jr stands for the decoded response and $x stands for the current for loop item.

With all that being said, Lever provides a description box when you create a job listing, which allows for simple html formatting and styling (bold, underline, ul, etc.). The problem is, in the process of retrieving and decoding the data inside the actual website, the html formatting is gone, meaning for example if the job listing contains a list with the job's requirements, I am not able to see it as a list, rather just joined text. Same goes for any bold or underline formatting.

Any ideas?

EDIT: Here's the code

For the GET Request:

    $url = "https://api.lever.co/v1/postings";
    
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    
    $headers = array(
        "Authorization: Basic apikeyherebutIhidit",
    );
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    
    $resp = curl_exec($curl);
    curl_close($curl);
    
    $jr = json_decode($resp, true);
    $jrlen = count($jr['data']);

For retrieving the job data (sample):

foreach ($categories as $category) {
    if($category->slug == "cruise") {
        $output .= '<div id="cruise" >';
            $output .='<div >' . '<div >';
                for ($x = 0; $x <= ($jrlen - 1); $x  ) { 
                    if ($jr['data'][$x]['categories']['department'] === 'PAX-Cruise Ships') { 
                        $output .=
                            '<div >'.
                                '<div >'.
                                    '<a  data-toggle="collapse" data-parent="#accordion-1" href="#accordion-one-link-'.$jr['data'][$x]['id'] .'">'.
                                        '<div >'.
                                            '<span >'. $jr['data'][$x]['text'] .'</span>'.
                                            '<i ></i>'.
                                        '</div>'.
                                    '</a>'.
                                '</div>'.
                                '<div id="accordion-one-link-'.$jr['data'][$x]['id'] .'" >'.
                                    '<div >'.
                                    '<div >' . $jr['data'][$x]['content']['description'] . '</div>' . 
                                    '<br>' . 
                                    '<input style="width:20px;"  type="checkbox" id="gdpr_checkbox_'.$jr['data'][$x]['id'].'" value="gdpr checked"><label for="gdpr_checkbox_'.$jr['data'][$x]['id'].'" style="font-weight:normal;font-size:15px;"> Please confirm that you have read and understood the <a style="color:#f5b14b;text-decoration: underline;" target="_blank" href="https://www.columbia-shipmanagement.com/general-privacy-notice/">General Privacy Notice</a> by checking the box.</label><br>' . 
                                    '<a id="apply_button_'.$jr['data'][$x]['id'].'"  type="submit" target="_blank" href="' . $jr['data'][$x]['urls']['apply'] . '" target="_blank" style="pointer-events: none;cursor: default;background-color:grey; border-color:grey;">Apply</a>' . 
                            '</div></div></div>';
                    }
                }
        $output .= '</div></div></div>';
    }
}

CodePudding user response:

If you check the documentation, https://hire.lever.co/developer/documentation#retrieve-a-single-posting, it becomes apparent real quick from the example given there, that they return a field description, which has any HTML stripped out - but that there is also a field descriptionHtml:

"content": {
  "description": "The Infrastructure Engineer will act as...\\nSuperman.",
  "descriptionHtml": "<div>The <u><b>Infrastructure Engineer</b></u> will act as...</div><div>Superman.</div>",
  • Related