Replacing characters in a returned Header Location in PHP

I’m trying to filter returned header location after POST using below PHP code. The returned header location is required for further processing of the payment status and saving the status in the db. The API provider seems not supportive since they don’t reply on time or fail to reply at all.

    $output = curl_exec($curl);
    $lines = explode("n",$output);
    $out = array();
    $headers = true;

    foreach ($lines as $l){
        $l = trim($l);
        if ($headers && !empty($l)){
           if (strpos($l,'location') !== false){
                $p = explode(' ',$l);
                $out['Headers']['location'] = trim($p[1]);
                $url = json_encode($out['Headers']['location']);
                echo json_encode($out['Headers']['location']);
            } 
        } 
    } 

The echo output is as below:- `"https://sandbox.kopokopo.com/api/v1/payments/c122c1d2-8e07-48d3-8c9d-597829447fda"`
How do I make the output to be a valid url without "" ? I'll really appreciate your valuable assistance.